WPF 数据网格行验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13995048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF Datagrid Row Validation
提问by user1864953
There seems to be a bug in WPF 4.0 DataGrids.
WPF 4.0 DataGrids 中似乎存在一个错误。
I'm implementing IDataErrorInfoon my objects, and I have an ObservableCollectionthat a datagrid binds to. I have ValidatesOnDataErrors=Trueset on the columns but nothing set on the rows. I have UpdateSourceTrigger="PropertyChanged"
我正在IDataErrorInfo我的对象上实现,并且我有ObservableCollection一个 datagrid 绑定到的对象。我ValidatesOnDataErrors=True在列上设置了但在行上没有设置。我有UpdateSourceTrigger="PropertyChanged"
The validation works perfectly on a cell by cell level. However, when you leave a cell invalid, go to any other cell and then return to the invalid cell and enter in valid data, the cell becomes valid but the row remains invalid when it should be valid.
验证在逐个单元格级别上完美运行。但是,当您使单元格无效时,转到任何其他单元格,然后返回到无效单元格并输入有效数据,单元格变为有效,但行在应该有效时仍然无效。
回答by Sankarann
In general property validation, it will be validated once the source gets updated but in the case of a RowValidationyou need to specify the RowValidationRuleto perform the RowValidation.
在一般属性验证中,它会在源更新后进行验证,但在 a 的情况下,RowValidation您需要指定RowValidationRule来执行RowValidation.
<DataGrid.RowValidationRules>
<DataErrorValidationRule ValidatesOnTargetUpdated="True" ValidationStep="UpdatedValue" />
</DataGrid.RowValidationRules>
Now the DataGridwill validates for the Rows also you can provide the RowValidationErrorTemplateto show the Error in Custom format.
现在DataGrid将验证行,您也可以提供RowValidationErrorTemplate以自定义格式显示错误。
回答by slimbofat
Had the same issue. The fix for me was setting ValidatesOnTargetUpdated="True"on the validation rule, which seems to force another validation every time the control gets updated.
有同样的问题。我的修复是ValidatesOnTargetUpdated="True"在验证规则上设置,这似乎在每次控件更新时强制进行另一次验证。

