1

在我的View.xaml 中,我有一个 DatePicker 绑定到 ViewModel 的 Date 属性

<DatePicker SelectedDate="{Binding Path=Date, Mode=TwoWay, Converter={StaticResource IfNullDateConverter}}" />

当 View.xaml 显示“IfNullConverter”用于将默认 DateTime 值“01.01.0001”转换为 DateTime.Now 时,DatePicker 显示当前日期。但实际上未设置 SelectedDate 属性。为什么?

当我按下保存按钮时,传递给 ViewModel 的 DatePicker 值仍然是“01.01.0001”。

请帮忙,我做错了什么?如果日期是“01.01.0001”,我如何更新我的源代码以传递当前日期?

IfNullDateConverter

DateTime dateValue = (DateTime)value;
            if (dateValue.ToShortDateString() == "01.01.0001")
            {
                return DateTime.Now;
            }
            else
            {
                return value; 
            }
4

1 回答 1

2

This is the right behavior. You should use converter when you want to convert the source value( Date in VM) and then set desdination(SelectedDate) with the new value. Which means that when you convert the source value, the result doesnt affect it, just the destination. Conclusion : if you want to set a default value DateTime.Now to your property, you should do it in your VM

于 2013-05-22T14:37:36.330 回答