【发布时间】:2013-10-11 20:50:34
【问题描述】:
我有一些代码具有意外行为,具体取决于您使用 DatePicker 控件的方式。如果您通过鼠标选择日期来使用 DatePicker,则事件会被触发一次并且代码可以正常工作。但是,如果您输入日期,则事件会触发两次。 这是我的 xaml 代码。
<DatePicker Margin="0,-1,0,0" Height="23" VerticalAlignment="Top" HorizontalAlignment="Right" Width="190" SelectedDate="{Binding DaDate}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectedDateChanged">
<i:InvokeCommandAction Command="{Binding SelectedDaDateChangedCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</DatePicker>
还有 c# 位。
public ICommand SelectedDaDateChangedCommand { get; set; }
private DateTime? daDate;
public DateTime? DaDate
{
get { return daDate; }
set
{
daDate = value;
NotifyOfPropertyChange(() => DaDate);
}
}
public MisoConstraintsViewModel(IView v) : base(v)
{
//This is where DoStuff() gets called twice from the SelectedDateChanged
SelectedDaDateChangedCommand = new RelayCommand(p => DoStuff());
}
感谢任何帮助。谢谢。
【问题讨论】:
-
什么时候被调用两次?你确定这不是因为输入时进行了 2 次更新 - 一次更改月份,一次更改日期 - 这很常见...
-
启动日期设置为 2013 年 10 月 9 日,然后我点击它并将其更改为 10/8/2013 并点击进入。每次看起来都会导致双重加载事件。
标签: c# wpf mvvm triggers datepicker