Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何通过在父类上实现IDataErrorInfo来验证子对象_C#_Wpf_Validation_Mvvm_Idataerrorinfo - Fatal编程技术网

C# 如何通过在父类上实现IDataErrorInfo来验证子对象

C# 如何通过在父类上实现IDataErrorInfo来验证子对象,c#,wpf,validation,mvvm,idataerrorinfo,C#,Wpf,Validation,Mvvm,Idataerrorinfo,我正在使用MVVM架构开发一个WPF应用程序。我是WPF的业余爱好者,所以请容忍我 我有两个模型课。父类具有另一(子)类的对象作为其属性。(我指的是嵌套对象,而不是继承对象) 例如,考虑下面的场景。 public class Company { public string CompanyName {get; set;} public Employee EmployeeObj {get; set;} } public class Employee { public st

我正在使用MVVM架构开发一个WPF应用程序。我是WPF的业余爱好者,所以请容忍我

我有两个模型课。父类具有另一(子)类的对象作为其属性。(我指的是嵌套对象,而不是继承对象)

例如,考虑下面的场景。

public class Company
{

   public string CompanyName {get; set;}

   public Employee EmployeeObj {get; set;}
}

public class Employee 
{

   public string FirstName {get; set;}

   public string LastName {get; set;}

}    
我想使用企业库验证块验证员工实体的属性

我可以通过在employee类中实现IDataErrorInfo接口来实现它,如下所示

public class Employee :  IDataErrorInfo

{

   [NotNullValidator(MessageTemplate="First Name is mandatory"]
   public string FirstName {get; set;}

   [StringLengthValidator(0,20,MessageTemplate="Invalid")]
   public string LastName {get; set;}

   public string Error
   {
        get
        {
            StringBuilder error = new StringBuilder();

            ValidationResults results = Validation.ValidateFromAttributes<Employee>(this);

            foreach (ValidationResult result in results)
            {
                error.AppendLine(result.Message);
            }

            return error.ToString();
        }

    }

    public string this[string propertyName]
    {
        get
        {
            ValidationResults results = Validation.ValidateFromAttributes<Employee>(this);

            foreach (ValidationResult result in results)
            {
                if (result.Key == propertyName)
                {
                    return result.Message;
                }
            }

            return string.Empty;
        }
    }
}
公共类员工:IDataErrorInfo
{
[NotNullValidator(MessageTemplate=“名字是必需的”]
公共字符串名{get;set;}
[StringLengthValidator(0,20,MessageTemplate=“无效”)]
公共字符串LastName{get;set;}
公共字符串错误
{
得到
{
StringBuilder错误=新建StringBuilder();
ValidationResults=Validation.ValidateFromAttributes(此);
foreach(结果中的ValidationResult)
{
错误。追加行(结果。消息);
}
返回错误。ToString();
}
}
公共字符串此[string propertyName]
{
得到
{
ValidationResults=Validation.ValidateFromAttributes(此);
foreach(结果中的ValidationResult)
{
if(result.Key==propertyName)
{
返回结果消息;
}
}
返回字符串。空;
}
}
}
我不想为我创建的每个子模型实现IDataErrorInfo

有没有办法通过在父(公司)类上实现IDataErrorInfo来验证Employee对象


还有启动对象验证的触发器。我只想在需要时验证对象,而不是一直验证对象。

您完全可以使用验证应用程序块在基类上实现
IDataErrorInfo
。这说明了如何进行验证。代码基本上归结为:

public abstract class DataErrorInfo : IDataErrorInfo
{
    string IDataErrorInfo.Error
    {
        get { return string.Empty; }
    }

    string IDataErrorInfo.this[string columnName]
    {
        get
        {
            var prop = this.GetType().GetProperty(columnName);
            return this.GetErrorInfo(prop); 
        }
    }

    private string GetErrorInfo(PropertyInfo prop)
    {
        var validator = this.GetPropertyValidator(prop);

        if (validator != null)
        {
           var results = validator.Validate(this);

           if (!results.IsValid)
           {
              return string.Join(" ",
                  results.Select(r => r.Message).ToArray());
           }
        }

        return string.Empty;
    }

    private Validator GetPropertyValidator(PropertyInfo prop)
    {
        string ruleset = string.Empty;
        var source = ValidationSpecificationSource.All;
        var builder = new ReflectionMemberValueAccessBuilder();
        return PropertyValidationFactory.GetPropertyValidator(
            this.GetType(), prop, ruleset, source, builder);
    }
}
可以使用此抽象类通过继承将验证行为添加到实体:

public partial class Customer : DataErrorInfo
{
}

请注意,IDataErrorInfo应该在ViewModel中实现,而不是在模型中实现。是的,这是一个选项。但如果我这样做,我必须在ViewModel中为模型中的每个属性创建一个属性。现在,我将模型对象本身作为属性,并将视图控件直接绑定到模型。