简体   繁体   English

是否可以结合使用 ValidationRules 和 INotifyDataErrorInfo 进行 wpf 验证?

[英]Is is possible to use a combination of ValidationRules and INotifyDataErrorInfo for wpf validation?

In WPF there are 3 ways to do validation:在 WPF 中,有 3 种方法可以进行验证:

  • Validation Rules验证规则
  • IDataErrorInfo
  • INotifyDataErrorInfo

Is it possible to use a combination of these at the same time?是否可以同时使用这些的组合? For my needs, I would like to validate new rules with the flexibility of INotifyDataErrorInfo, but don't want to interfere with existing ValidationRules for the same object I want to validate.根据我的需要,我想使用 INotifyDataErrorInfo 的灵活性来验证新规则,但不想干扰我要验证的同一对象的现有 ValidationRules。

1. Mixing the different source of errors 1. 混合不同的错误来源

Yes you can mix the three kind of Validation you quote :是的,您可以混合使用您引用的三种验证:

  • ValidationRules are fine for GUI(surface) validation ValidationRules适用于 GUI(表面)验证
  • IDataErrorInfo is implemented on the view model/business object. IDataErrorInfo在视图模型/业务对象上实现。 It is fine for more business oriented validation更适合面向业务的验证
  • INotifyDataErrorInfo is also implemented on the view model/business object. INotifyDataErrorInfo也在视图模型/业务对象上实现。 It adds possibility of multiple errors on a given field and also adds asynchronous validations (ie a server or a thread can take time to answer to the valildation).它增加了在给定字段上出现多个错误的可能性,并增加了异步验证(即服务器或线程可能需要时间来回答验证)。

That last source of error is really more longer to implement最后一个错误源的实现时间确实更长

2. Taking errors into account 2. 考虑错误

The most difficult is to take into account the different sources of error when you want to prevent a window from being closed if data is not valid.最困难的是在数据无效时要防止窗口被关闭时考虑不同的错误来源。

ValidationRules error must be looked up in the GUI bindings, because the invalid data doesn't get to the business object/ViewModel.必须在 GUI 绑定中查找 ValidationRules 错误,因为无效数据无法到达业务对象/ViewModel。

IDataErrorInfo and INotifiDataErrorInfo can be looked in the ViewModel layer. IDataErrorInfoINotifiDataErrorInfo可以在 ViewModel 层中查看。

Regards问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM