我正嘗試在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