我正尝试在WPF + MVVM中使用IDataErrorInfo进行验证。我遵循MSDN关于如何实现它的文章。问题是,我如何处理VM上的传递属性?关于虚拟机上的直通属性的WPF MVVM验证
例如,
public class A : INotifyPropertyChanged, IDataErrorInfo
{
protected string _Name;
public string Name
{
get
{
return _Name;
}
set
{
_Name = value;
OnPropertyChanged("Name");
}
}
public string this[string propertyName]
{
get
{
string result = null;
if (propertyName == "Name")
{
if (Name == "ABC")
{
result = "Name cannot be ABC";
}
}
return result;
}
}
}
public class ViewModel : INotifyPropertyChanged
{
A a = new A();
public string ModelName
{
get
{
return a.Name;
}
set
{
a.Name = value;
OnNameChanged();
OnPropertyChanged("ModelName");
}
}
}
<TextBox Name="txtName" Text="{Binding Path=ModelName, ValidatesOnDataErrors=True}" />
我有什么做的视图模型,这样我就不必对视图模型再次重新验证Name属性?
谢谢
你是什么意思再'“?如果你想验证一次,你可以定义一个布尔属性(例如:'hasNameValidated')。 – Haritha 2013-04-04 04:31:13