Microsoft Windows Presentation Foundation (WPF) has a rich data-binding system. In addition to being a key driver for loosely coupling UI definitions from support logic and data through the Model-view-viewmodel (MVVM) model, the data-binding system provides strong and flexible support for business data validation scenarios. The data binding mechanism in WPF includes several options for evaluating the validity of input data when creating an editable view. In addition, you can easily customize the way users are instructed to verify errors by using WPF templates and style features for controls.
In order to support complex rules and display validation errors to the user, it is often necessary to combine the various validation mechanisms available. Even seemingly simple forms of data entry can lead to validation challenges when business rules become complex. Common scenarios involve simple rules for a single attribute level and cross coupling properties, in which the validity of one property depends on the value of another property. However, with validation support in WPF data binding, these challenges can be easily addressed.
In this article, you will learn how to meet data validation needs with IDataErrorInfo interface implementations, Validationrules, bindinggroups, exceptions, and additional properties and events related to validation. You will also learn how to use your own errortemplates and tooltips to customize the display of validation errors. In this article, I assume you are familiar with the basic data binding capabilities of WPF.
Data Validation Overview
Almost every time you enter or modify data in your application, you need to ensure that the data is valid to avoid a far cry from the source (in this case, the user) of these changes. Also, you need to give them clear instructions when the data entered by the user is not valid, and to provide them with instructions on how to correct the data. As long as you know what features to use and when to use them, you can accomplish these tasks fairly easily through WPF.
When you use data binding in WPF to render business data, you should typically use the Binding object to provide a data pipeline between a single property of the target control and the properties of the data source object. To make validation relevant, you typically need to twoway data binding-which means that, in addition to the data that flows from the source property to the target property for display, the edited data flows from the target to the source, as shown in Figure 1.
Figure 1 TwoWay data flow in data binding
You can use three mechanisms to determine whether data entered through a data-bound control is valid. The mechanisms are summarized in Figure 2.
Figure 2 Binding validation mechanism
Validation mechanism |
Description |
Abnormal |
By setting the Validatesonexceptions property on a Binding object, if an exception is thrown during an attempt to set the modified value on the Source object property, a validation error is set for the Binding. |
Validationrules |
The Binding class has a property that provides a collection of instances of ValidationRule derived classes. These validationrules need to overwrite a Validate method that is invoked by Binding when the data in the bound control changes. If the Validate method returns an invalid Validationresult object, the validation error is set for that Binding. |
IDataErrorInfo |
By implementing the IDataErrorInfo interface on the bound data source object and setting the Validatesondataerrors property on the Binding object, Binding invokes the IDataErrorInfo API exposed from the bound data source object. If a non-null or non-null string is returned from these property calls, a validation error is set for the Binding. |