In the previous article we discussed the knowledge points about WPF bindings, and now we can easily bind business data as a source to WPF controls and can then be presented in a specific style by creating different data template. And as the most commonly used feature we can interact with the interface through the two way binding pattern, and at this point we need something similar to the validator in asp.net to limit or verify the validity of the data. ValidationRule was born for such an application. This article discusses in detail how to apply validation rules and custom error templates.
Let's start by looking at several common classes in WPF that relate to data validation rules:
· ValidationRule: base class for all custom validation rules. Provides a portal for user-defined validation rules.
· Exceptionvalidation: Represents a rule that checks for exceptions that are thrown during the binding source property update. It is a built-in rule that checks for exceptions that are thrown during the binding source property update.
· Validationresult: The presentation of data validation results. After the Validate method of the ValidationRule object is executed, the validationresult is used to represent the result of the validation. This contains the error message-errorcontent, and whether the data is valid-isvalid. Validresult is a valid instance of Validationresult.
· ValidationError: Represents a validation error that is created by the binding engine when ValidationRule reports validation errors.
For validation and conversion values for bindings in WPF, we need to be aware of:
1. When the value is transferred from the target property to the Source property, the data binding engine first removes any validationerror that may have been added to the Validation.errors attached property of the bound element. The data binding engine then checks to see if a custom validation rule is defined for the Binding, and if a custom validation rule is defined, it calls the Validate method on each ValidationRule until one of the rules fails or all the rules are passed. If a custom rule fails, the binding engine creates a ValidationError object and adds the object to the Validation.errors collection of the bound element. If Validation.errors is not empty, the Validation.haserror attached property of the element is set to true. Additionally, if the NotifyOnValidationError property of Binding is set to true, the binding engine raises the Validation.error attached event on that element.
2. If all rules are passed, the binding engine invokes the converter (if it exists).
3. If the converter passes, the binding engine invokes the setter of the Source property.
4. If the binding has a exceptionvalidationrule associated with it, and an exception is thrown in step 3 or 4, the binding engine checks for the presence of Updatesourceexceptionfilter. Use the Updatesourceexceptionfilter callback to provide a custom handler for handling exceptions. If Updatesourceexceptionfilter is not specified for Binding, the binding engine creates a validationerror on the exception and adds it to the Validation.errors collection of the bound element.
A valid value transfer operation in any direction (target to source or source to destination) clears the Validation.errors attached property.