【发布时间】:2012-01-15 17:54:44
【问题描述】:
我有一个文本绑定到如下属性
用户需要输入文件名。然而,有时用户可能会输入无效字符。所以我在 View Model 中的支持属性如下所示
private string outputFileName;
public string OutputFileName
{
get
{
return outputFileName;
}
set
{
string temp = value;
if (true == IsValidFileName(temp))// this function uses Path.Getinvalidfilechars
{
outputFileName = value;
}
else
{
MessageBox.Show(string.Format("{0} contains one or more invalid characters
for a file Name",temp));
}
base.OnPropertyChanged("OutputFileName");
}
}
这是问题所在,文本框仍然显示无效字符。为什么 OnPropertyChanged 不会导致文本框中的文本返回到没有无效字符的旧值。 我怎样才能得到这种行为
【问题讨论】:
-
o.O 请删除
true ==... -
来自 setter 的 MessageBox 是......有点吓人......更好的方法可能是接受该值,并单独验证。就数据绑定而言,它只是成功地分配了值,因此它认为它已经“知道”该值
-
为什么会有任何不同?
-
@Rahul 发给谁?
标签: c# wpf data-binding