代码之家  ›  专栏  ›  技术社区  ›  Andy Clarke

WPF IDataErrorInfo和NHibernate验证-如何触发验证?

  •  0
  • Andy Clarke  · 技术社区  · 17 年前

    我最近在我的应用程序中插入了NHibernate验证,我用NHibernate属性修饰了我的域对象的属性,所以。。。

        [NotEmpty, Length(Min = 1, Max = 40)]
        public string Description { get; set; }
    

        public string this[string columnName]
        {
            get
            {
                var result = new ValidatorEngine().Validate(this);
                _invalidValues = result.Where(x => x.PropertyName == columnName).Select(x => x.Message);
                return _invalidValues.FirstOrDefault();
            }
    
        }
    
        public string Error
        {
            get
            {
                return string.Empty;
            }
        }
    

    XAML看起来像这样

     <TextBox Grid.Row="0" Grid.Column="3" Text="{Binding Path=Entity.Description, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" IsEnabled="{Binding IsEditable}" ></TextBox>
    

    我打算写一个方法来使用反射,并将属性设置为它们已经相等的值,以便触发验证,但这似乎是一个特别有效的方法!!

    有人能让我重回正轨吗?

    安迪

    2 回复  |  直到 17 年前
        2
  •  1
  •   Andy Clarke    17 年前

    这是因为属性为Null,我需要另一个NHibernate验证修饰符来考虑这一点(NotNullNotEmpty),而不是我使用的NotEmpty。

     [NotNullNotEmpty, Length(Min = 1, Max = 40)]
     public string Description { get; set; }
    
    推荐文章