“必需” DataAnnotation 抛出未处理的异常
我正在尝试使用 Silverlight 4 中的 DataAnnotations 来验证用户输入。
在此示例中,一切都按预期进行:
<TextBox x:Name="txtName" Margin="15,0,0,0" MinWidth="200" Height="Auto" Text="{Binding Name, Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True}"/>
使用此 ViewModel 代码:
#region Name
private string name;
[Display(Name="Pet Name", Description="Here goes the pet's name")]
[StringLength(50, ErrorMessage="Name must be 3 - 50 characters", MinimumLength=3)]
public string Name
{
get
{
return name;
}
set
{
Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "Name" });
this.name= value;
this.RaisePropertyChanged("Name");
this.AceptarCommand.OnCanExecuteChanged();
}
}
#endregion
当我尝试添加“必需” DataAnnotation:
区域名称
private string name;
[Display(Name="Pet Name", Description="Here goes the pet's name")]
[Required(ErrorMessage="You must write a name")]
[StringLength(50, ErrorMessage="Name must be 3 - 50 characters", MinimumLength=3)]
public string Name
{
get
{
return name;
}
set
{
Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "Name" });
this.name= value;
this.RaisePropertyChanged("Name");
this.AceptarCommand.OnCanExecuteChanged();
}
}
#endregion
ValidateProperty 执行时,它会在页面第一次加载时抛出未处理的异常(它没有默认值)。
我做错了什么?
提前致谢
I'm trying to use DataAnnotations in Silverlight 4 to validate user inputs.
In this example, everything goes as expected:
<TextBox x:Name="txtName" Margin="15,0,0,0" MinWidth="200" Height="Auto" Text="{Binding Name, Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True}"/>
With this ViewModel code:
#region Name
private string name;
[Display(Name="Pet Name", Description="Here goes the pet's name")]
[StringLength(50, ErrorMessage="Name must be 3 - 50 characters", MinimumLength=3)]
public string Name
{
get
{
return name;
}
set
{
Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "Name" });
this.name= value;
this.RaisePropertyChanged("Name");
this.AceptarCommand.OnCanExecuteChanged();
}
}
#endregion
The problem comes when I try to add a "Required" DataAnnotation:
region Name
private string name;
[Display(Name="Pet Name", Description="Here goes the pet's name")]
[Required(ErrorMessage="You must write a name")]
[StringLength(50, ErrorMessage="Name must be 3 - 50 characters", MinimumLength=3)]
public string Name
{
get
{
return name;
}
set
{
Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "Name" });
this.name= value;
this.RaisePropertyChanged("Name");
this.AceptarCommand.OnCanExecuteChanged();
}
}
#endregion
When ValidateProperty executes, it throws an unhandled exception when the page loads the first time (it has no default value).
What am I doing wrong??
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最后我发现了问题。
这是因为我在 DataContext 构造函数中初始化文本框绑定值(名称),因此它在构造所有内容之前抛出异常(可能是 VisualTree...)。
所以你必须在施工后执行此操作。
Finally I found the problem.
This was caused because I was initializing textbox binded value (Name) in DataContext constructor so it threw the exception before everything was constructed (maybe visualtree...).
So you have to do this AFTER construction.