验证规则存在问题
我正在尝试获取验证规则以返回错误。我在模型中实现了 IDataErrorInfo,其中包含我的业务对象属性以及在事件验证失败时返回的消息。我还创建了一个验证规则。问题是,验证规则正在触发(已将其添加为书签),但规则中的 IDataErrorInfo 引用从未出现错误,即使我的模型的 IDataErrorInfo 实现生成了错误。数据网格明确显示验证失败。
我通过让规则和模型返回两条不同的消息来测试它,并且始终返回模型的版本。就像我的规则看不到 IDataErrorInfo 对象中的内容,或者它只是创建它的新实例。
DataGrid:
<DataGrid ItemsSource="{Binding Path=ProjectExpenseItemsCollection}" AutoGenerateColumns="False"
Name="dgProjectExpenseItems" RowStyle="{StaticResource RowStyle}"
SelectedItem="{Binding Path=SelectedProjectExpenseItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
CanUserDeleteRows="True" CanUserAddRows="True">
<DataGrid.RowValidationRules>
<vr:RowDataInfoValidationRule ValidationStep="UpdatedValue" />
</DataGrid.RowValidationRules>
<DataGrid.Columns>
<DataGridTextColumn Header="Item Number"
Binding="{Binding ItemNumber, Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" />
</DataGrid.Columns>
</DataGrid>
验证规则:
对象“idei”不为 null,但 idei.Error 始终是零长度字符串 ("")
public class RowDataInfoValidationRule : ValidationRule
{
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
BindingGroup bindingGroup = (BindingGroup)value;
IDataErrorInfo idei = bindingGroup.Items[0] as IDataErrorInfo;
string error = (idei != null) ? idei.Error : null;
return (string.IsNullOrEmpty(error)) ? ValidationResult.ValidResult : new ValidationResult(false, error + ": ValidationRule");
}
}
模型/业务对象:
public class ProjectExpenseItemsBO : IDataErrorInfo, IEditableObject, INotifyPropertyChanged
{
public string ItemNumber { get; set; }
public ProjectExpenseItemsBO() {}
// string method
static bool IsStringMissing(string value)
{
return String.IsNullOrEmpty(value) || value.Trim() == String.Empty;
}
#region IDataErrorInfo Members
public string Error
{
get { return this[string.Empty]; }
}
public string this[string propertyName]
{
get
{
string result = string.Empty;
if (propertyName == "ItemNumber")
{
if (IsStringMissing(this.ItemNumber))
{
result = "Item number cannot be empty-IDataError!";
}
}
return result;
}
}
#endregion
}
I am trying to get a validation rule to return an error. I implemented IDataErrorInfo in my model, which contains my business object properties and messages to return in the event validation fails. I also created a validation rule. The problem is, the validation rule is firing (bookmarked it) but the IDataErrorInfo reference in the rule never has an error, even though the IDataErrorInfo implementation of my model generates one. The datagrid definitely shows there was a validation failure.
I tested it by having the rule and model return two different messages, and the model's version is always returned. It is like my rule cannot see what is in the IDataErrorInfo object, or it is just creating a new instance of it.
DataGrid:
<DataGrid ItemsSource="{Binding Path=ProjectExpenseItemsCollection}" AutoGenerateColumns="False"
Name="dgProjectExpenseItems" RowStyle="{StaticResource RowStyle}"
SelectedItem="{Binding Path=SelectedProjectExpenseItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
CanUserDeleteRows="True" CanUserAddRows="True">
<DataGrid.RowValidationRules>
<vr:RowDataInfoValidationRule ValidationStep="UpdatedValue" />
</DataGrid.RowValidationRules>
<DataGrid.Columns>
<DataGridTextColumn Header="Item Number"
Binding="{Binding ItemNumber, Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" />
</DataGrid.Columns>
</DataGrid>
Validation Rule:
The object "idei" isn't null, but idei.Error is always a zero-length string ("")
public class RowDataInfoValidationRule : ValidationRule
{
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
BindingGroup bindingGroup = (BindingGroup)value;
IDataErrorInfo idei = bindingGroup.Items[0] as IDataErrorInfo;
string error = (idei != null) ? idei.Error : null;
return (string.IsNullOrEmpty(error)) ? ValidationResult.ValidResult : new ValidationResult(false, error + ": ValidationRule");
}
}
Model/Business Object:
public class ProjectExpenseItemsBO : IDataErrorInfo, IEditableObject, INotifyPropertyChanged
{
public string ItemNumber { get; set; }
public ProjectExpenseItemsBO() {}
// string method
static bool IsStringMissing(string value)
{
return String.IsNullOrEmpty(value) || value.Trim() == String.Empty;
}
#region IDataErrorInfo Members
public string Error
{
get { return this[string.Empty]; }
}
public string this[string propertyName]
{
get
{
string result = string.Empty;
if (propertyName == "ItemNumber")
{
if (IsStringMissing(this.ItemNumber))
{
result = "Item number cannot be empty-IDataError!";
}
}
return result;
}
}
#endregion
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该规则获取的 IDataErrorInfo 对象将是 ProjectExpenseItemsBO 对象的实例。您检查的唯一属性是 Error,您已实现它以返回
this[string.Empty]
,它将始终返回 string.Empty。您可能想要更改 Error 属性的实现以查看对象中的所有错误,或者让 RowDataInfoValidationRule 迭代属性并通过索引器获取每个错误的错误消息。您从模型中收到验证错误,因为与 ItemNumber 的绑定将 ValidatesOnDataErrors 设置为 True,因此框架将使用属性名称 ItemNumber 调用索引器并获取错误消息。
The IDataErrorInfo object that the rule gets will be an instance of your ProjectExpenseItemsBO object. The only property you check is Error, which you have implemented to return
this[string.Empty]
, which will always return string.Empty. You probably either want to change your implementation of the Error property to look at all the errors in the object, or to have RowDataInfoValidationRule iterate through properties and get the error message for each one through the indexer.You are getting validation errors from the model because your binding to ItemNumber has ValidatesOnDataErrors set to True, so the framework will call the indexer with the property name ItemNumber and get your error message.