WPF Datagrid多行验证 [英] WPF Datagrid Multiple Row Validation
问题描述
很抱歉,如果已经提出此问题,但我无法找到我的特定问题的答案.
Apologies if this question has already been asked, but I have not been able to find an answer to my specific problem.
我有一个WPF datagrid
,它绑定到称为 Waypoint
的对象的集合中,该对象实现了 IDataErrorInfo
.每个 Waypoint
对象都有一组具有 DataItem
的属性,这些属性也实现了 IDataErrorInfo
.
I have a WPF datagrid
which is bound to a Collection of objects called Waypoint
that implements IDataErrorInfo
. Each Waypoint
object a set of properties that have DataItem
which also implements IDataErrorInfo
.
数据网格中的每一列都绑定到 DataItem
对象的Value属性,而我想要的是将 Waypoint
对象绑定到行验证模板和 DataItem
对象绑定到单元验证器.
Each column in the datagrid is bound to the Value property of the DataItem
object, and what I want is for the Waypoint
object to be bound to the Row Validation template and the DataItem
object to be bound to the Cell Validator.
我有一个 RowValidationRule
如下:
<DataGrid.RowValidationRules>
<DataErrorValidationRule ValidationStep="UpdatedValue" ValidatesOnTargetUpdated="True"/>
</DataGrid.RowValidationRules>
我已经部分地工作了,但是仅当我离开该行时才显示行验证,如果行出错则更令人沮丧的是,验证规则不会在随后的任何行上执行,这不是我所要做的想.我尝试查看datagrid参考中的代码,以查看是否可以在 CommitEdit
方法中进行任何重写来触发验证规则,但是我很困惑.
I have got this partially working, but the row validation is only displayed when I navigate away from the row and more frustratingly if a row has an error then the validation rule is not executed on any subsequent rows, which is not what I want. I have tried looking at the code in the datagrid reference to see if I can do any overriding in the CommitEdit
method to fire the validation rule, but I am stumped.
我们放置了替代项,以便在网格有任何验证错误的情况下都可以编辑单元格.由于默认情况下,除非清除了所有错误,否则网格不应该是可编辑的,我猜测未显示的多行验证错误是设计使然的?如果有人对如何解决这个问题有任何想法,将不胜感激!!!
We have put in the override so that cells can be edited if the grid has any validation errors. As by default the grid isn't supposed to be editable until any errors have been cleared I am guessing that multiple row validation errors not being displayed is by design? If anyone has any ideas on how I could get round this it would be greatly appreciated!!!
推荐答案
第二个问题-
更令人沮丧的是,如果某行有错误,则验证规则不会在随后的任何行上执行,这不是我想要的
more frustratingly if a row has an error then the validation rule is not executed on any subsequent rows, which is not what I want
DataGrid如果存在验证错误,则实际上不会提交该行.我在Microsoft参考代码的 OnExecutedCommitEdit 方法中找到了这段代码-
DataGrid does not actually commit the row if it has validation error. I found this piece of code in OnExecutedCommitEdit method in the Microsoft reference code-
if (validationPassed)
{
CommitRowItem();
}
依次执行此操作-
private void CommitRowItem()
{
if (IsEditingRowItem)
{
EditableItems.CommitEdit();
}
else
{
EditableItems.CommitNew();
// Show the placeholder again
UpdateNewItemPlaceholder(/* isAddingNewItem = */ false);
}
}
为解决此问题,我扩展了DataGrid以制作自己的自定义DataGrid并覆盖了 OnExecutedCommitEdit ,如下所示-
To overcome this problem, I extended the DataGrid to make my own custom DataGrid and overrode OnExecutedCommitEdit as following -
protected override void OnExecutedCommitEdit(ExecutedRoutedEventArgs e)
{
base.OnExecutedCommitEdit(e);
BindingFlags bindingFlags = BindingFlags.FlattenHierarchy | BindingFlags.NonPublic | BindingFlags.Instance;
PropertyInfo editableItems = this.GetType().BaseType.GetProperty("EditableItems", bindingFlags);
((System.ComponentModel.IEditableCollectionView)editableItems.GetValue(this)).CommitEdit();
}
此解决方案似乎有效,到目前为止,我还没有遇到任何问题.
This solution seems to work and I have not faced any issues till now.
这篇关于WPF Datagrid多行验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!