C# WPF-用于在运行时设置日期和时间的时间选择器绑定
在我的Wpf应用程序中,我有两个计时器。我已经为它们使用了绑定,但它们的时间没有更新。我还想为这些时间选择器设置所选日期。我尝试如下绑定它。但是,这是行不通的。这里的实际问题是时间选择器在DataTemplate中 这里是xaml:C# WPF-用于在运行时设置日期和时间的时间选择器绑定,c#,wpf,datatemplate,timepicker,C#,Wpf,Datatemplate,Timepicker,在我的Wpf应用程序中,我有两个计时器。我已经为它们使用了绑定,但它们的时间没有更新。我还想为这些时间选择器设置所选日期。我尝试如下绑定它。但是,这是行不通的。这里的实际问题是时间选择器在DataTemplate中 这里是xaml: <DataTemplate x:Key="EditableDataTemplate"> <StackPanel Orientation="Horizontal" Width="596"> <xctk:TimePicker N
<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>
请提出一些建议?您正在尝试在属性设置器中设置值,这些设置器将永远不会被调用,因此属性值将永远不会被设置(因此永远不会通知,时间选择器也永远不会更新) 为什么不尝试在ViewModel的构造函数中设置属性呢。我不确定您的ViewModel叫什么,但下面是:
public class MyViewModel : INotifyPropertyChanged
{
public MyViewModel()
{
StartValue = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
EndValue = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
}
private DateTime _dateTime1;
public DateTime StartValue
{
get
{
return _dateTime1;
}
set
{
if (_dateTime1.Equals(value)) return;
_dateTime1 = value;
OnPropertyChanged("StartValue");
}
}
private DateTime _dateTime2;
public DateTime EndValue
{
get
{
return _dateTime2;
}
set
{
if (_dateTime2.Equals(value)) return;
_dateTime2 = value;
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
}
这将确保为这两个属性触发PropertyChanged事件,并且只要绑定正确,就会看到值更新。在哪个类中声明了属性。您是否使用ViewModel?在哪里使用EditableDataTemplate?请将信息添加到问题中调试时输出窗口中是否有绑定错误?@Jehof Yes。这些属性在我的ViewModel中声明。在我的xaml中,我有两个数据模板。“EditableDataTemplate”是其中之一。@Richard没有收到任何错误。只有计时器选择器不更新。其他控件工作正常。如果在setters中设置断点,是否可以确认它们是否被命中?我将冒险猜测它们不是,因此属性值永远不会被设置(因此永远不会被通知,时间选择器也永远不会更新)。如果您从其他地方设置这些属性的值,例如VM的构造函数,会发生什么情况?是否需要在TimePicker的xaml中编写RelativeSource?窗口的DataContext设置为什么?这需要设置为您的viewmodel。窗口上的其他绑定是否有效?是的。它仅在我的构造函数中设置为ViewModel。其他绑定可以工作。那么,如果去掉RelativeSource,会出现任何绑定错误吗?使用RelativeSource,它会更新我不需要的每个条目的StartValue和EndValue。因此,我删除了它并将UpdateSourceTrigger=PropertyChanged放在那里。但是,我没有收到任何绑定错误。
public class MyViewModel : INotifyPropertyChanged
{
public MyViewModel()
{
StartValue = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
EndValue = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
}
private DateTime _dateTime1;
public DateTime StartValue
{
get
{
return _dateTime1;
}
set
{
if (_dateTime1.Equals(value)) return;
_dateTime1 = value;
OnPropertyChanged("StartValue");
}
}
private DateTime _dateTime2;
public DateTime EndValue
{
get
{
return _dateTime2;
}
set
{
if (_dateTime2.Equals(value)) return;
_dateTime2 = value;
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
}