[英]WPF IDataErrorInfo and NHibernate Validation - how to trigger a Validate?
I recently plugged the NHibernate validation into my app, I've decorated the properties of my domain objects with the NHibernate attributes as so ... 我最近将NHibernate验证插入到我的应用程序中,因此已经使用NHibernate属性修饰了域对象的属性...
[NotEmpty, Length(Min = 1, Max = 40)]
public string Description { get; set; }
I've also implemented IDataErrorInfo on my Domain Object ... 我还在我的域对象上实现了IDataErrorInfo ...
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;
}
}
The XAML looks like this XAML看起来像这样
<TextBox Grid.Row="0" Grid.Column="3" Text="{Binding Path=Entity.Description, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" IsEnabled="{Binding IsEditable}" ></TextBox>
My issue is that when I create a new instance of my Domain object then the validate is not being called, as effectively the properties (such as the Description in my example) have not changed. 我的问题是,当我创建我的Domain对象的新实例时,由于没有有效地更改属性(如示例中的Description),因此没有调用validate。
I was going to write a method to use reflection and set the properties to what they are already equal to in order to trigger the validate, but this dosnt seem a particularly efficient approach!! 我打算编写一种使用反射并将属性设置为已经等于以触发验证的方法,但这似乎是一种特别有效的方法!
Can someone put me back on track please? 有人可以让我回到正轨吗?
Cheers, 干杯,
Andy 安迪
Paul Stovell for has an excellent article for validation of business objects 保罗·斯托维尔 ( Paul Stovell)撰写了一篇有关验证业务对象的出色文章
http://www.codeproject.com/KB/cs/DelegateBusinessObjects.aspx http://www.codeproject.com/KB/cs/DelegateBusinessObjects.aspx
It was because the properties were Null and I needed another NHibernate validation decorator to take account of that (NotNullNotEmpty) rather than the NotEmpty I had used. 这是因为属性为Null,因此我需要另一个NHibernate验证修饰器来考虑该问题(NotNullNotEmpty),而不是我曾经使用的NotEmpty。
[NotNullNotEmpty, Length(Min = 1, Max = 40)]
public string Description { get; set; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.