在我的 Wpf 应用程序中,我有两个 TimePicker。我已经为他们使用了绑定,但他们的时间没有更新。我还想为这些 TimePickers 设置选定的日期。我尝试如下绑定它。但是,它不会起作用。这里的实际问题是 TimePickers 在 DataTemplate 中。
这是xaml:
<DataTemplate x:Key="EditableDataTemplate">
<StackPanel Orientation="Horizontal" Width="596">
<xctk:TimePicker Name="StartPicker" Value="{Binding StartValue, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" 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, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" Format="Custom" FormatString="hh:mm tt" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" Width="60" EndTime="11:59:0"/>
</StackPanel>
</DataTemplate>
在 C# 中与属性绑定:
private DateTime _dateTime1;
public DateTime StartValue
{
get
{
return _dateTime1;
}
set
{
_dateTime1 = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
OnPropertyChanged("StartValue");
}
}
private DateTime _dateTime2;
public DateTime EndValue
{
get
{
return _dateTime2;
}
set
{
_dateTime2 = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
OnPropertyChanged("EndValue");
}
}
protected virtual void OnPropertyChanged(String time)
{
if (System.String.IsNullOrEmpty(time))
{
return;
}
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(time));
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
请提出一些想法?