windows8 使用ValidationAttribute对CheckBox验证必须

当对CheckBox使用ValidationAttribute验证时,系统没有针对bool做True的验证。需要自己写一个。代码如下:

    public class MustBeTrueAttribute : ValidationAttribute
    {
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (value is bool && (bool)value == true)
                return ValidationResult.Success;


            return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
        }
    }

 

posted @ 2015-06-02 17:07  咆哮  阅读(212)  评论(0编辑  收藏  举报