HasErrors:一个只读的boolean属性,这分辨如果对象作为一个整体时是否有验证error;
GetErrors:该方法对于一个给定的属性返回验证error;
ErrorsChanged:当发生新的error——或缺乏error——被检测到。就会生成这一事件。
需要说明的是,如果你返回false到HasErrors属性时,如果没有出现error就会发生绑定。
WPF4.5怎样使用?
您必须在每个绑定到你的对象ValidatesOnDataErrors属性设置为true。
在示例中创建了一个表单,显示对象的属性命名为“Person”。这里将解释如何在绑定中启用INotifyDataErrorInfo验证:
<TextBox Text="{Binding Name,Mode=TwoWay,ValidatesOnNotifyDataErrors=True}"/>
该绑定将自己为ErrorsChanged事件进行注册。每当绑定属性事件发生时,这些控制将自动显示error。前面已经指出,这只会出现在当HasErrors被设置为true时。
在验证Person类时发生了延迟。在设置任何属性时验证都将发生,但它会因“WaitSecondsBeforeValidation ”而延迟数秒。
保存每个属性下面的error并通过GetErros方法来提供。如果有其中的error出现那么HasErrors属性将返回为true。以下是实现代码:
public System.Collections.IEnumerable GetErrors(string propertyName)
{
List<string> errorsForName;
_errors.TryGetValue("Name", out errorsForName);
return errorsForName;
}
public bool HasErrors
{
get { return _errors.Values.FirstOrDefault(l => l.Count > 0) != null; }
}
private Dictionary<string, List<string>> _errors =
new Dictionary<string, List<string>>();
private void Validate()
{
Task waitTask = new Task(() => Thread.Sleep(
TimeSpan.FromSeconds(WaitSecondsBeforeValidation)));
waitTask.ContinueWith((_) => RealValidation());
waitTask.Start();
}
private object _lock = new object();
private void RealValidation()
{
lock (_lock)
{
//Validate Name
List<string> errorsForName;
if (!_errors.TryGetValue("Name", out errorsForName))
errorsForName = new List<string>();
else errorsForName.Clear();
if (String.IsNullOrEmpty(Name))
errorsForName.Add("The name can't be null or empty.");
_errors["Name"] = errorsForName;
if (errorsForName.Count > 0) RaiseErrorsChanged("Name");
//Validate Age
List<string> errorsForAge;
if (!_errors.TryGetValue("Age", out errorsForAge))
errorsForAge = new List<string>();
else errorsForAge.Clear();
if (Age <= 0)
errorsForAge.Add("The age must be greater than zero.");
_errors["Age"] = errorsForAge;
if (errorsForAge.Count > 0) RaiseErrorsChanged("Age");
}
{
List<string> errorsForName;
_errors.TryGetValue("Name", out errorsForName);
return errorsForName;
}
public bool HasErrors
{
get { return _errors.Values.FirstOrDefault(l => l.Count > 0) != null; }
}
private Dictionary<string, List<string>> _errors =
new Dictionary<string, List<string>>();
private void Validate()
{
Task waitTask = new Task(() => Thread.Sleep(
TimeSpan.FromSeconds(WaitSecondsBeforeValidation)));
waitTask.ContinueWith((_) => RealValidation());
waitTask.Start();
}
private object _lock = new object();
private void RealValidation()
{
lock (_lock)
{
//Validate Name
List<string> errorsForName;
if (!_errors.TryGetValue("Name", out errorsForName))
errorsForName = new List<string>();
else errorsForName.Clear();
if (String.IsNullOrEmpty(Name))
errorsForName.Add("The name can't be null or empty.");
_errors["Name"] = errorsForName;
if (errorsForName.Count > 0) RaiseErrorsChanged("Name");
//Validate Age
List<string> errorsForAge;
if (!_errors.TryGetValue("Age", out errorsForAge))
errorsForAge = new List<string>();
else errorsForAge.Clear();
if (Age <= 0)
errorsForAge.Add("The age must be greater than zero.");
_errors["Age"] = errorsForAge;
if (errorsForAge.Count > 0) RaiseErrorsChanged("Age");
}
最终,我创建了一个减低验证事件error发生的RaiseErrorsChanged方法:
public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;
public void RaiseErrorsChanged(string propertyName)
{
EventHandler<DataErrorsChangedEventArgs> handler = ErrorsChanged;
if (handler == null) return;
var arg = new DataErrorsChangedEventArgs(propertyName);
handler.Invoke(this, arg);
}
public void RaiseErrorsChanged(string propertyName)
{
EventHandler<DataErrorsChangedEventArgs> handler = ErrorsChanged;
if (handler == null) return;
var arg = new DataErrorsChangedEventArgs(propertyName);
handler.Invoke(this, arg);
}