【发布时间】:2021-02-25 16:06:46
【问题描述】:
我正在使用带有 MVVM 模式的 WPF,我在 XAML 中有一个 DatePicker,如下所示:
<DatePicker Text="{Binding FCGene,Mode=TwoWay}" SelectedDateFormat="Short" />
在视图的构造函数和视图模型的构造函数中,我设置了这样的文化
CultureInfo ci = CultureInfo.CreateSpecificCulture(CultureInfo.CurrentCulture.Name);
ci.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";
ci.DateTimeFormat.LongDatePattern = "dd/MM/yyyy HH:mm:ss";
Thread.CurrentThread.CurrentCulture = ci;
当我选择像13/02/2021 这样的日期时,我收到了这个错误:
“数值无法转换”
在DatePicker 文本框的正下方,我看到了dd/MM/yyyy 格式的文本框中的日期,这正是我想要的。
我猜问题出在绑定中,在属性的分配中,在我的视图模型中,我的属性是这样的:
private DateTime fcGene;
public DateTime FCGene
{
get { return fcGene; }
set { SetProperty(ref fcGene, value); }
}
【问题讨论】: