Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

In WPF there are 3 ways to do validation:

  • 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.

like image 862
disklosr Avatar asked Nov 10 '15 12:11

disklosr


People also ask

What is the use of inotifydataerrorinfo in WPF?

Validation using INotifyDataErrorInfo Added in .NET 4.5 for WPF It works on the same principle as IDataErrorInfo but it is based on the event driven mechanism which notifies for the validation errors asynchronously based on the input from some time consuming operation like service call.

What is WPF validation exception and validation rules?

WPF validation using Validation Exception and Validation rules. Added in .NET 4.5 for WPF It works on the same principle as IDataErrorInfo but it is based on the event driven mechanism which notifies for the validation errors asynchronously based on the input from some time consuming operation like service call.

How to do multiple validations on the same property in WPF?

This article explains multiple validations on the same property in WPF using INotifyDataErrorInfo and Data annotation. From WPF 4.5, Microsoft added support of INotifyDataErrorInfo validation from WPF. The model we want to validate now can implement this interface and can do multiple validations for the property. It also supports aync validation.

What is the difference between validationrule and validate method?

The Validate method returns the ValidationResult based on the Success/Fail indication. It also returns the error message. Once a validation rule is defined. We bind this rule to the ValidationRule collection of the Binding class. ValidationRules can be used to encapsulate the reuse-able rules. The downside is that they are not context specific.


1 Answers

1. Mixing the different source of errors

Yes you can mix the three kind of Validation you quote :

  • ValidationRules are fine for GUI(surface) validation
  • IDataErrorInfo is implemented on the view model/business object. It is fine for more business oriented validation
  • INotifyDataErrorInfo is also implemented on the view model/business object. It adds possibility of multiple errors on a given field and also adds asynchronous validations (i.e. 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

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.

IDataErrorInfo and INotifiDataErrorInfo can be looked in the ViewModel layer.

Regards

like image 83
Emmanuel DURIN Avatar answered Sep 30 '22 03:09

Emmanuel DURIN