我的疑问很简单,如何在 WPF 4.5 中使用此 INotifyDataErrorInfo 显示异常?
我正在使用 MVVM:
这是我观点的一部分
<TextBox MinHeight="50"
Text="{Binding Person.Name, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True}"
这里是我的模型课。检查我设置@字符的Validate方法应该抛出异常
public class Person : DomainObject
{
private string _name;
public string Name
{
get
{
return this._name;
}
set
{
if (this._name != value)
{
this.ValidateProperty("Name", value);
this._name = value;
this.RaisePropertyChanged("Name");
}
}
}
}
protected override void ValidateProperty(string propertyName, object value)
{
if (propertyName == "Name")
{
var errors = new List<string>();
var response = value as string;
if (string.IsNullOrEmpty(response))
{
errors.Add("The value cannot be null or empty");
}
else if (response == "@")
{
throw new Exception("@");
}
this.ErrorsContainer.SetErrors(propertyName, errors);
}
else
{
base.ValidateProperty(propertyName, value);
}
}
当这种情况发生时,它真的会停止程序。据我所知,在 Silverlight 中不会发生这种情况。