WPF Datepicker 使用 MVVM 返回先前选择的日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20659070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF Datepicker returns previously selected Date using MVVM
提问by Deepak Pathak
I am working on a WPF project where I have a Datepicker control on a Window. I am using MVVM pattern for data bindings. I have setup a command on SelectedDate changed event of the Datepicker. The problem is that for e.g., when first time I change the date I get NULLin the event handler of the command. And when I changed the date again, then I get the previously selected date in the event handler. This is a strange behaviour as this doesn't happens if I don't use WPF commands and work in code behind model.
我正在开发一个 WPF 项目,在该项目中,我在 Window 上有一个 Datepicker 控件。我正在使用 MVVM 模式进行数据绑定。我已经在 Datepicker 的 SelectedDate 更改事件上设置了一个命令。问题是,例如,当我第一次更改日期时,我在命令的事件处理程序中得到NULL。当我再次更改日期时,我会在事件处理程序中获得先前选择的日期。这是一种奇怪的行为,因为如果我不使用 WPF 命令并在模型背后的代码中工作,则不会发生这种情况。
Here is my xaml code snippet for DatePicker:
这是我用于 DatePicker 的 xaml 代码片段:
<DatePicker x:Name="dpEventDate" Grid.Row="1" Grid.Column="2" HorizontalAlignment="Stretch" Margin="150,0,150,0"
VerticalAlignment="Center" SelectedDateFormat="Long"
SelectedDate="{Binding Path=., Source={x:Static sys:DateTime.Today}, StringFormat=Today is {0:dddd, MMMM dd}}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectedDateChanged">
<i:InvokeCommandAction Command="{Binding SelectedDateChangedCommand}"
CommandParameter="{Binding ElementName=dpEventDate, Path=SelectedDate}" />
</i:EventTrigger>
</i:Interaction.Triggers>
<DatePicker.Background>
<SolidColorBrush Color="LightYellow"></SolidColorBrush>
</DatePicker.Background>
</DatePicker>
Command event handler in ViewModel class
ViewModel 类中的命令事件处理程序
get
{
return new DelegateCommand((sdate) =>
{
selectedDate = Convert.ToDateTime(sdate);
if (sdate != null)
{
//some logic here
}
}
}
set
{
SelectedDateChangedCommand = value;
RaisePropertyChangedEvent("SelectedDateChangedCommand");
}
DelegateCommand class implements ICommand
DelegateCommand 类实现 ICommand
So in above code "sdate" parameter always return a previously selected date. And if it is the first time I change the date it returns NULL.
所以在上面的代码中,“ sdate”参数总是返回一个先前选择的日期。如果这是我第一次更改日期,它将返回NULL。
Any idea what I might be doing wrong?
知道我可能做错了什么吗?
回答by Tseng
I'm not sure what you are trying to achieve by binding the change to the event and command. Is there a reason why you don't bind the SelectedDateProperty of your DatePicker (using TwoWaymode)? Currently you're binding it to a static DateTime.Today.
我不确定您要通过将更改绑定到事件和命令来实现什么。是否有理由不绑定SelectedDateDatePicker的属性(使用TwoWay模式)?目前,您将它绑定到一个静态的DateTime.Today.
<DatePicker x:Name="dpEventDate" Grid.Row="1" Grid.Column="2" HorizontalAlignment="Stretch" Margin="150,0,150,0"
VerticalAlignment="Center" SelectedDateFormat="Long"
SelectedDate="{Binding MyDateTimeProperty, Mode=TwoWay}">
and in your ViewModel having a
并且在您的 ViewModel 中有一个
private Nullable<DateTime> myDateTimeProperty = null;
public Nullable<DateTime> MyDateTimeProperty {
get {
if(myDateTimeProperty == null) {
myDateTimeProperty = DateTime.Today;
}
return myDateTimeProperty;
}
set {
myDateTimeProperty = value;
RaisePropertyChangedEvent("MyDateTimeProperty");
}
}
This way your ViewModel will return the current date and set it in the DatePicker, if the date is null previously and if the DatePicker is changed, it will bind it's new way back to the property.
这样,您的 ViewModel 将返回当前日期并将其设置在 DatePicker 中,如果该日期之前为空并且 DatePicker 已更改,它将以新的方式将其绑定回属性。

