为TimePicker(扩展工具包 - WPF)控件设置选定的日期

时间:2013-08-08 14:26:12

标签: c# wpf timepicker

在我的WPF应用程序中,我想为TimePicker控件设置选定的日期。在我的应用程序中,我在我的一个View窗口中使用日历。当我从日历中选择日期并更改TimePicker值(即时间)并保存时,我的条目仅保存到今天的日期。我想将我的条目保存到所选日期。那么,如何在这种情况下为TimePicker控件设置选择日期?

XAML:

<StackPanel Orientation="Horizontal" Width="596">
   <TextBox Name="CltText" Text="{Binding ClientNameBinding,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="145"/>
   <TextBox Name="AppText" Text="{Binding ApplicationNameBinding}" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="90"/>
   <xctk:TimePicker Name="StartPicker" Value="{Binding StartValue, ElementName=MainWin, Mode=TwoWay}" Format="Custom" FormatString="hh:mm tt" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" Width="100" EndTime="11:59:0"/>
   <xctk:TimePicker Name="EndPicker" Value="{Binding EndValue, ElementName=MainWin, Mode=TwoWay}" Format="Custom" FormatString="hh:mm tt" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" Width="60" EndTime="11:59:0"/>
   <TextBox Name="TaskText" Text="{Binding TaskNameBinding}" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="71"/>
   <ComboBox x:Name="ProjectComboBox" ItemsSource="{Binding Path=projectList, ElementName=MainWin}" SelectedValuePath="_id" DisplayMemberPath="_name"  SelectedItem="{Binding ProjectNameBindingClass, Mode=OneWayToSource}" Width="130" Background="Yellow" BorderThickness="0"/>
</StackPanel>

2 个答案:

答案 0 :(得分:0)

您的TimePicker返回一个可以为空的DateTime对象,因此如果您已经有一个包含日期信息的DateTime对象,并且您想将它与包含时间信息的第二个DateTime对象组合,则可以执行DateTime.Parse,将它们输出为字符串:

//date is your existing Date object, time is the nullable DateTime object from your TimePicker
var dateTime = DateTime.Parse(string.Format("{0} {1}", date.Date.ToShortDateString(), time.Value.ToLongtTimeString()));

答案 1 :(得分:0)

我得到了解决方案。

StartTimeText和StopTimeText是我的Timepickers的名称。下面的行将为这些TimePickers设置日期和时间。

StartTimeText.Value = new DateTime(DateTime.Now.Year,DateTime.Now.Month,DateTime.Now.Day,DateTime.Now.Hour,DateTime.Now.Minute,DateTime.Now.Second);
StopTimeText.Value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);