如何从 DateTimePicker 中提取日期和时间(以小时、分钟、秒、星期几、日、月为单位)? Xceed 的 WPF 扩展工具包

How to Extract Date and time ( in Hours, Minutes, Seconds, DayofWeek, Day, Month) from DateTimePicker? WPF extended toolkit by Xceed

我有 WPF Extended Toolkit 3.5.0(由 Xceed 开发),我正在使用此处的 DateTimePicker 控件。我想知道如何通过 DateTimePicker 分别获取小时、分钟、秒、星期几、日、月的选定值。 在此之前我使用的是 telerik DateTimePicker 控件在这里我使用这种方法来获取这些值

hours= DTPicker.SelectedTime.Value.Hours;
minutes= DTPicker.SelectedTime.Value.Minutes;
seconds= DTPicker.SelectedTime.Value.Seconds;
//for date
dayofweek= DTPicker.SelectedDate.Value.DayOfWeek;
day= DTPicker.SelectedDate.Value.Day;
month= DTPicker.SelectedDate.Value.Month;

但我在这里找不到 SelectedDate/SelectedTime/SelectedValue 属性。 谢谢

Value属性returns一个DateTime?:

DateTime? value = dateTimePicker.Value;
if (value.HasValue)
{
    hours = value.Value.TimeOfDay.Hours;
    minutes = value.Value.TimeOfDay.Minutes;
    seconds = value.Value.TimeOfDay.Seconds;
    //for date
    dayofweek = value.Value.DayOfWeek;
    day = value.Value.Day;
    month = value.Value.Month;
}

您基本上可以在当前代码中将 SelectedTime 替换为 Value

假设您已将工具包 DateTiemPicker 命名为 DTPicker。

<toolkits:DateTimePicker Value="{Binding LastUpdated}" Format="FullDateTime" Width="200" Height="30" Name="DTPicker"/>

然后你可以得到你需要的所有值如下。

var seledtedDateTime = DTPicker.Value;
if (seledtedDateTime.HasValue)
{
    var hours = seledtedDateTime.Value.Hour;
    var minutes = seledtedDateTime.Value.Minute;
    var seconds = seledtedDateTime.Value.Second;
    var dayofweek = seledtedDateTime.Value.DayOfWeek;
    var day = seledtedDateTime.Value.Day;
    var month = seledtedDateTime.Value.Month;
}