如何在WPF或Winforms应用程序中使用System.ComponentModel.DataAnnotations [英] How to use System.ComponentModel.DataAnnotations in WPF or Winforms application

查看:49
本文介绍了如何在WPF或Winforms应用程序中使用System.ComponentModel.DataAnnotations的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用 System.ComponentModel.DataAnnotations 及其所属属性(例如, Required Range ,...)WPF还是Winforms类?

is it possible to use System.ComponentModel.DataAnnotations and it's belong attribute(such as Required,Range,...) in WPF or Winforms class?

我想将验证信息归因于

谢谢

我写这个:

public class Recipe
{
    [Required]
    [CustomValidation(typeof(AWValidation), "ValidateId", ErrorMessage = "nima")]
    public int Name { get; set; }
}

private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        var recipe = new Recipe();
        recipe.Name = 3;
        var context = new ValidationContext(recipe, serviceProvider: null, items: null);
        var results = new List<System.ComponentModel.DataAnnotations.ValidationResult>();

        var isValid = Validator.TryValidateObject(recipe, context, results);

        if (!isValid)
        {
            foreach (var validationResult in results)
            {
                MessageBox.Show(validationResult.ErrorMessage);
            }
        }
    }

public class AWValidation
{
    public bool ValidateId(int ProductID)
    {
        bool isValid;

        if (ProductID > 2)
        {
            isValid = false;
        }
        else
        {
            isValid = true;
        }

        return isValid;
    }        
}

但即使我将3设置为我的财产,也什么也没发生

but even I set 3 to my property nothing happend

推荐答案

是的,您可以.这是另一篇文章,对此进行了说明.您甚至可以在控制台应用程序中通过手动创建 ValidationContext 来执行此操作:

Yes, you can. And here's another article illustrating this. You could do this even in a console application by manually creating a ValidationContext:

public class DataAnnotationsValidator
{
    public bool TryValidate(object @object, out ICollection<ValidationResult> results)
    {
        var context = new ValidationContext(@object, serviceProvider: null, items: null);
        results = new List<ValidationResult>();
        return Validator.TryValidateObject(
            @object, context, results, 
            validateAllProperties: true
        );
    }
}


更新:

这是一个例子:

public class Recipe
{
    [Required]
    [CustomValidation(typeof(AWValidation), "ValidateId", ErrorMessage = "nima")]
    public int Name { get; set; }
}

public class AWValidation
{
    public static ValidationResult ValidateId(int ProductID)
    {
        if (ProductID > 2)
        {
            return new ValidationResult("wrong");
        }
        else
        {
            return ValidationResult.Success;
        }
    }
}

class Program
{
    static void Main()
    {
        var recipe = new Recipe();
        recipe.Name = 3;
        var context = new ValidationContext(recipe, serviceProvider: null, items: null);
        var results = new List<ValidationResult>();

        var isValid = Validator.TryValidateObject(recipe, context, results, true);

        if (!isValid)
        {
            foreach (var validationResult in results)
            {
                Console.WriteLine(validationResult.ErrorMessage);
            }
        }
    }
}

请注意, ValidateId 方法必须是公共静态的,并返回 ValidationResult 而不是布尔值.还要注意传递给 TryValidateObject 方法的第四个参数,如果要评估自定义验证器,则必须将其设置为true.

Notice that the ValidateId method must be public static and return ValidationResult instead of boolean. Also notice a fourth argument passed to the TryValidateObject method which must be set to true if you want your custom validators to be evaluated.

这篇关于如何在WPF或Winforms应用程序中使用System.ComponentModel.DataAnnotations的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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