1
驗證WPF 4.5中的異常嗎?我的疑問很簡單,我如何在WPF 4.5中使用此INotifyDataErrorInfo顯示異常?你知道如何使用INotifyDataErrorInfo
我使用MVVM:
這是我的看法
<TextBox MinHeight="50"
Text="{Binding Person.Name, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True}"
在這裏,我的模型類的一部分。檢查驗證方法,在這裏我設置@字符,應該拋出一個異常
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不會發生此。
您能否提供更多的上下文? – 2012-04-02 22:16:54
@TravisJ信息添加! – 2012-04-02 22:23:48