WPF Datepicker使用MVVM返回先前选择的日期 [英] WPF Datepicker returns previously selected Date using MVVM
问题描述
这是我的DatePicker的xaml代码片段:
< DatePicker x:Name =dpEventDate Row =1Grid.Column =2HorizontalAlignment =StretchMargin =150,0,150,0
VerticalAlignment =CenterSelectedDateFormat =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>
ViewModel类中的命令事件处理程序 / p>
get
{
return new DelegateCommand((sdate)=>
{
selectedDate = Convert.ToDateTime(sdate);
if(sdate!= null)
{
//某些逻辑在这里
}
}
}
set
{
SelectedDateChangedCommand = value;
RaisePropertyChangedEvent(SelectedDateChangedCommand);
}
DelegateCommand类实现ICommand
所以在上面的代码 sdate 参数始终返回以前选择的日期,如果是第一次更改日期返回 NULL 。
任何想想我可能做错了什么?
我不知道你试图通过将变更绑定到事件和命令这就是为什么你不绑定DatePicker的 SelectedDate
属性(使用 TwoWay
模式)的原因)目前,您将它绑定到一个静态的 DateTime.Today
。
< DatePicker x:Name =dpEventDateGrid.Row =1Grid.Column =2HorizontalAlignment = StretchMargin =150,0,150,0
VerticalAlignment =CenterSelectedDateFormat =Long
SelectedDate ={Binding MyDateTimeProperty,Mode = TwoWay}>
在您的ViewModel中有一个
private Nullable< DateTime> myDateTimeProperty = null;
public Nullable< DateTime> MyDateTimeProperty {
get {
if(myDateTimeProperty == null){
myDateTimeProperty = DateTime.Today;
}
return myDateTimeProperty;
}
set {
myDateTimeProperty = value;
RaisePropertyChangedEvent(MyDateTimeProperty);
}
}
这样您的ViewModel将返回当前日期并设置它在DatePicker中,如果以前的日期为空,并且如果DatePicker被更改,它将以新的方式绑定到该属性。
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 NULL in 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.
Here is my xaml code snippet for DatePicker:
<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
get
{
return new DelegateCommand((sdate) =>
{
selectedDate = Convert.ToDateTime(sdate);
if (sdate != null)
{
//some logic here
}
}
}
set
{
SelectedDateChangedCommand = value;
RaisePropertyChangedEvent("SelectedDateChangedCommand");
}
DelegateCommand class implements 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.
Any idea what I might be doing wrong?
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 SelectedDate
Property of your DatePicker (using TwoWay
mode)? Currently you're binding it to a static 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
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.
这篇关于WPF Datepicker使用MVVM返回先前选择的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!