自定义ValidationAttribute不起作用。总是返回true [英] Custom ValidationAttribute doesn't work. Always returns true

查看:1620
本文介绍了自定义ValidationAttribute不起作用。总是返回true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个自定义ValidationAttribute类检查一个人的年龄在我的应用程序:

I've created a custom ValidationAttribute class to check the age of a person in my application:

public class MinimumAgeAttribute : ValidationAttribute
{
    public int MinAge { get; set; }

    public override bool IsValid(object value)
    {
        return CalculateAge((DateTime) value) >= MinAge;
    }

    private int CalculateAge(DateTime dateofBirth)
    {
        DateTime today = DateTime.Now;
        int age = today.Year - dateofBirth.Year;
        if (dateofBirth > today.AddYears(-age)) age--;
        return age;
    }
}



数据注解设置在这样的领域:

The data annotation is set on the field like this:

[MinimumAge(MinAge = 18, ErrorMessage = "Person must be over the age of 18")]   
public DateTime DateOfBirth;



在我的UI绑定设置是这样的:

The binding in my UI is set like this:

<DatePicker SelectedDate="{Binding SelectedPerson.DateOfBirth, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" Grid.Column="1"/>

当我设置日期(例如),以2007年9月6日例如, Validator.TryValidateObject 总是返回true。

When I set the date (for example) to 09/06/2007 for example, Validator.TryValidateObject always returns true.

为什么呢?这似乎只影响我的自定义类,在System.ComponentModel.DataAnnotations提供的所有那些做工精细。

Why? This only seems to affect my custom classes, all the ones provided in System.ComponentModel.DataAnnotations work fine.

推荐答案

之所以您的自定义ValidationAttribute类是没有工作是因为WPF没有(默认)在进行验证时考虑这样的类。进行验证的默认机制正在实施IDataErrorInfo的(可用于.NET 4.0和更早版本)或INotifyDataErrorInfo(介绍了.NET 4.5)接口。如果你不希望任何实现这些接口,那么你可以创建一个有效性规则,但我更喜欢实现上面提到的接口。

The reason why your custom ValidationAttribute class isn't working is because WPF doesn't (by default) look into such classes when doing validation. The default mechanism for validation is implementing the IDataErrorInfo (available for .NET 4.0 and earlier) or INotifyDataErrorInfo (introduced in .NET 4.5) interfaces. If you don't want to implement any of those interfaces, then you can create a ValidationRule, but I prefer implementing the interfaces mentioned above.

您可以找到很多的如何做到这一点联机,但做一个快速搜索示例发现这个的博客文章(这在快速扫描我觉得是非常透彻)。

You can find a lot of examples on how to do this online, but doing a quick search found this blog post (which on a quick scan I felt was very thorough).

既然你似乎对使用数据更敏锐注释代替IDataErrorInfo的/ INotifyDataErrorInfo接口或有效性规则,我认为微软TechNet文章的数据验证在MVVM是一个非常干净,彻底实现使用Data注解验证。我通过阅读解决方案,我和会推荐给其他人。

Since you seemed more keen on using Data Annotations instead of IDataErrorInfo/INotifyDataErrorInfo interfaces or Validation Rule, I think the Microsoft TechNet article "Data Validation in MVVM" is a very clean and thorough implementation of using Data Annotations for validation. I read through the solution myself and would recommend it to others.

这篇关于自定义ValidationAttribute不起作用。总是返回true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆