“必需” DataAnnotation 抛出未处理的异常

发布于 2024-12-01 21:27:57 字数 1813 浏览 2 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

硪扪都還晓 2024-12-08 21:27:57

最后我发现了问题。

这是因为我在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文