【发布时间】:2012-04-21 21:14:18
【问题描述】:
我希望用户能够输入例如310312 并自动将日期选择器的文本属性更新为 31/03/12;我已将日期选择器绑定到视图模型的“日期”属性,如下所示。
在 WPF4.0 中,绑定现在会在设置后自动执行获取(不需要 INotifyPropertyChanged);这发生在下面的代码中,但虽然“get”日期字段值是正确的“31/03/12”,但 datepicker 文本属性没有更新,并保持在 310312 (NB UpdateSourceTrigger=PropertyChanged)。
文本框属性确实发生了变化(例如,未显示的设置代码转换为大写)
我真的很感激一些关于为什么会这样的提示。
<Grid>
<DatePicker Text="{Binding Path=Date,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
Height="25" HorizontalAlignment="Left" Name="datePicker1"/>
<TextBox Text="{Binding Path=State,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Height="23" HorizontalAlignment="Left" Name="textBox1" />
</Grid>
private string date;
public string Date
{
get
{
return date;
}
set
{
if (value != null)
{
Regex abbreviatedDateFormat = new Regex(@"\A\d{6}\Z");
if (abbreviatedDateFormat.IsMatch(value))
{
value = value.Insert(2, "/");
value = value.Insert(5, "/");
}
}
date = value;
}
}
【问题讨论】:
-
我想达到同样的效果。你找到解决办法了吗?
标签: wpf