问题
I am trying to use a Datepicker + a Timepicker together to return a DateTime that can be stored in a database. For example I would like to have a StartDate and an EndDate (if applicable) to a Meeting scheduled.
How would I go about combining the values into the correct format that a SQL database would handle.
Any feedback would be great.
回答1:
I got this to work.
String dateTimeString = "";
DateTime dateTime = DateTime.MinValue;
dateTimeString = DateTime.Parse(datePicker.Value.ToString()).ToString("MM/dd/yyyy") + " " +
DateTime.Parse(timePicker.Value.ToString()).ToString("h:mm tt");
dateTime = DateTime.Parse(dateTimeString);
I don't know why but it throws an exception if you try to format from the Value Ie datePicker.Value.ToString("MM/dd/yyyy")
回答2:
I was looking for more of an all-inclusive answer to this question, so this is what I have put together that works pretty well for me.
First we create a user control that wraps the datepicker and timepicker controls together. In the constructor you can specify the type of control you would like to display (DATE, TIME, OR DATE_TIME). Both pickers will then be bound to the same date property within the control. When being used, the usercontrol's date value can be set and get using the "TheDate" property of the control.
DateTimeControl dtc = new DateTimeControl("DATE_TIME");
dtc.TheDate = DateTime.Now;
<UserControl
x:Class="IFS.FSMW.Architecture.Controls.DateTimeControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:IFS.FSMW.Architecture.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<DatePicker Margin="0,0,10,0" x:Name="theDatePicker"/>
<TimePicker Grid.Column="1" x:Name="theTimePicker"/>
</Grid>
public sealed partial class DateTimeControl : UserControl
{
public String ControlType
{
get { return (String)GetValue(ControlTypeProperty); }
set { SetValue(ControlTypeProperty, value); }
}
// Using a DependencyProperty as the backing store for ControlType. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ControlTypeProperty =
DependencyProperty.Register("ControlType", typeof(String), typeof(DateTimeControl), new PropertyMetadata(""));
public DateTime TheDate
{
get { return (DateTime)GetValue(TheDateProperty); }
set { SetValue(TheDateProperty, value); }
}
// Using a DependencyProperty as the backing store for TheDate. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TheDateProperty =
DependencyProperty.Register("TheDate", typeof(DateTime), typeof(DateTimeControl), new PropertyMetadata(DateTime.Now));
public DateTimeControl()
{
this.InitializeComponent();
}
public DateTimeControl(String ControlTypeIn)
{
this.InitializeComponent();
ControlType = ControlTypeIn;
switch (ControlType.ToUpper())
{
case "DATE_TIME":
this.theDatePicker.Visibility = Windows.UI.Xaml.Visibility.Visible;
this.theTimePicker.Visibility = Windows.UI.Xaml.Visibility.Visible;
break;
case "DATE":
this.theDatePicker.Visibility = Windows.UI.Xaml.Visibility.Visible;
this.theTimePicker.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
break;
case "TIME":
this.theDatePicker.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
this.theTimePicker.Visibility = Windows.UI.Xaml.Visibility.Visible;
break;
default:
break;
}
DateTimeToDateTimeOffsetConverter converter = new DateTimeToDateTimeOffsetConverter();
Binding bdDate = new Binding();
bdDate.Mode = BindingMode.TwoWay;
bdDate.Converter = converter;
bdDate.Path = new PropertyPath("TheDate");
bdDate.Source = this;
Binding bdTime = new Binding();
bdTime.Mode = BindingMode.TwoWay;
bdTime.Converter = converter;
bdTime.ConverterParameter = TheDate;
bdTime.Path = new PropertyPath("TheDate");
bdTime.Source = this;
if (ControlType.ToUpper() == "DATE_TIME")
{
theDatePicker.SetBinding(DatePicker.DateProperty, bdDate);
theTimePicker.SetBinding(TimePicker.TimeProperty, bdTime);
}
else if (ControlType.ToUpper() == "DATE")
{
theDatePicker.SetBinding(DatePicker.DateProperty, bdDate);
}
else if (ControlType.ToUpper() == "TIME")
{
theTimePicker.SetBinding(TimePicker.TimeProperty, bdTime);
}
}
}
public class DateTimeToDateTimeOffsetConverter :IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
try
{
if (targetType == typeof(TimeSpan))
{
if (parameter != null)
{
DateTime dt = (DateTime)value;
//Get the timespan from subtracting the date from the original DateTime
//this returns a timespan representing the time component of the DateTime
TimeSpan ts = dt - dt.Date;
return ts;
}
else
{
return new DateTimeOffset(DateTime.Now);
}
}
DateTime date = (DateTime)value;
return new DateTimeOffset(date);
}
catch (Exception ex)
{
return DateTimeOffset.MinValue;
}
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
try
{
if (value.GetType() == typeof(TimeSpan))
{
TimeSpan ts = (TimeSpan)value;
DateTime dateParam = (DateTime)parameter;
return new DateTime(dateParam.Year,dateParam.Month,dateParam.Day,ts.Hours,ts.Minutes,ts.Seconds);
}
DateTimeOffset dto = (DateTimeOffset)value;
return dto.DateTime;
}
catch (Exception ex)
{
return DateTime.MinValue;
}
}
}
回答3:
try this,
DateTime myDate= ((DateTime)DatePicker.Value).Date.Add(((DateTime)TimePicker.Value).TimeOfDay);
回答4:
You can accomplish this by binding both the DatePicker and the TimePicker to the same DateTime value. The TimePicker would bind to the DateTime.TimeOfDay property
<DatePicker Header="Date" Date="{Binding Date}"/>
<TimePicker Header="Time" Time="{Binding Date.TimeOfDay}"/>
回答5:
@user3216360's answer put me on the right track to solving this for my own application. Unfortunately, the bindings he suggested kept resulting in the Date picker being reset to the initial value each time the Time picker value was changed. I took a slightly different approach and hooked up two additional dependency properties to solve the issue. Those two dependency properties can then update the SelectedDateTime property. So far, it seems to be working great!
using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
namespace MyApp.Views
{
public sealed partial class DateTimePickerControl : UserControl
{
public static readonly DependencyProperty SelectedDateTimeProperty =
DependencyProperty.Register("SelectedDateTime", typeof (DateTime), typeof (DateTimePickerControl), new PropertyMetadata(DateTime.Now));
public static readonly DependencyProperty SelectedDateProperty =
DependencyProperty.Register("SelectedDate", typeof (DateTimeOffset), typeof (DateTimePickerControl), new PropertyMetadata(DateTimeOffset.Now, OnDateChanged));
public static readonly DependencyProperty SelectedTimeProperty =
DependencyProperty.Register("SelectedTime", typeof (TimeSpan), typeof (DateTimePickerControl), new PropertyMetadata(TimeSpan.FromHours(12), OnTimeChanged));
public DateTimePickerControl()
{
InitializeComponent();
var bdDate = new Binding
{
Mode = BindingMode.TwoWay,
Path = new PropertyPath("SelectedDate"),
Source = this
};
var bdTime = new Binding
{
Mode = BindingMode.TwoWay,
Path = new PropertyPath("SelectedTime"),
Source = this
};
PART_DatePicker.SetBinding(DatePicker.DateProperty, bdDate);
PART_TimePicker.SetBinding(TimePicker.TimeProperty, bdTime);
}
public DateTimeOffset SelectedDate
{
get { return (DateTimeOffset) GetValue(SelectedDateProperty); }
set { SetValue(SelectedDateProperty, value); }
}
public DateTime SelectedDateTime
{
get { return (DateTime) GetValue(SelectedDateTimeProperty); }
set { SetValue(SelectedDateTimeProperty, value); }
}
public TimeSpan SelectedTime
{
get { return (TimeSpan) GetValue(SelectedTimeProperty); }
set { SetValue(SelectedTimeProperty, value); }
}
private static void OnDateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var instance = d as DateTimePickerControl;
if (instance == null)
{
return;
}
var dto = (DateTimeOffset) e.NewValue;
TimeSpan ts = instance.SelectedTime;
instance.SelectedDateTime = new DateTime(dto.Year, dto.Month, dto.Day, ts.Hours, ts.Minutes, ts.Seconds);
}
private static void OnTimeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var instance = d as DateTimePickerControl;
if (instance == null)
{
return;
}
DateTimeOffset dto = instance.SelectedDate;
var ts = (TimeSpan) e.NewValue;
instance.SelectedDateTime = new DateTime(dto.Year, dto.Month, dto.Day, ts.Hours, ts.Minutes, ts.Seconds);
}
}
}
回答6:
Try
DateTime myDate = DatePickerControl.Date.Date.Add(TimePickerControl.Time);
来源:https://stackoverflow.com/questions/23250452/combining-datepicker-and-timepicker-values-win-8-1