结合Datepicker和Timepicker值Win 8.1

时间:2014-04-23 16:40:42

标签: c# xaml datepicker windows-store-apps timepicker

我正在尝试一起使用Datepicker + Timepicker来返回可以存储在数据库中的DateTime。例如,我希望在计划的会议中安装StartDate和EndDate(如果适用)。

我如何将值组合成SQL数据库将处理的正确格式。

任何反馈都会很棒。

6 个答案:

答案 0 :(得分:1)

您可以通过将DatePicker和TimePicker绑定到相同的DateTime值来实现此目的。 TimePicker将绑定到DateTime.TimeOfDay属性

<DatePicker Header="Date" Date="{Binding Date}"/>
<TimePicker Header="Time" Time="{Binding Date.TimeOfDay}"/>

答案 1 :(得分:1)

我让这个工作。

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);

我不知道为什么但是如果您尝试从值datePicker.Value.ToString("MM/dd/yyyy")

进行格式化,则会引发异常

答案 2 :(得分:1)

我正在寻找更多关于这个问题的全包式答案,所以这就是我把它放在一起对我来说非常好。

首先,我们创建一个用户控件,将datepicker和timepicker控件包装在一起。在构造函数中,您可以指定要显示的控件类型(DATE,TIME或DATE_TIME)。然后两个选择器将绑定到控件中的相同日期属性。使用时,可以设置用户控件的日期值,并使用&#34; TheDate&#34;控制的财产。

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 :(得分:0)

试试这个,

DateTime myDate= ((DateTime)DatePicker.Value).Date.Add(((DateTime)TimePicker.Value).TimeOfDay);

答案 4 :(得分:0)

@ user3216360的答案让我走上了正确的道路,为我自己的应用程序解决了这个问题。不幸的是,他建议的绑定会导致每次更改时间选择器值时将日期选择器重置为初始值。我采用了稍微不同的方法,并连接了两个额外的依赖属性来解决问题。然后,这两个依赖项属性可以更新SelectedDateTime属性。到目前为止,它似乎工作得很好!

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);
        }
    }
}

答案 5 :(得分:0)

尝试

DateTime myDate = DatePickerControl.Date.Date.Add(TimePickerControl.Time);