WPF Data Validation

Kailash Chandra Behera | Wednesday, January 25, 2017


Data Validation is most important part in every projects, this blog defines how data validation is handled in WPF.

Getting Started

Previously when we are developing projects in Winform, we had to check to each and every controls for data manually and normally we were using MessageBox dialog for displying error of each property or control. WPF provice an effective way for data validation and display errors of each property or control without using MessageBox dialog, Even we can display error message in bottom or right side of the control on which error occurred like web page for example.

For displaying an error message of each property with bound control, WPF provides support for managing data validation errors that occur when changing individual properties that are bound to controls in the view. For single properties that are data-bound to control, the view model or model can signal a data validation error within the property setter by rejecting an incoming bad value and throwing an exception.
If the ValidatesOnExceptions property on the data binding is true, the data binding engine in WPF will handle the exception and display a visual cue to the user that there is a data validation error.

WPF provides two interfaces for data validation that is listed below, these interfaces allow your view model or model to perform data validation for one or more property values and to return an error message to the view so that the user can be notified of the error.
  1. IDataErrorInfo:-This interface provides basic support for property data validation and error reporting. It defines two read-only properties: an indexer property, with the property name as the indexer argument, and an Error property. Both properties return a string value.
  2. INotifyDataErrorInfo:-The INotifyDataErrorInfo interface is more flexible than the IDataErrorInfo interface. It supports multiple errors for a property, asynchronous data validation, and the ability to notify the view if the error state changes for an object.

Related Articles

  1. WPF Data Validation: IDataErrorInfo
  2. WPF Round Corner Button with click effects
  3. Round Corner PasswordBox in WPF
  4. Round Corner TextBox in WPF
  5. WPF Custom Datagrid Control(Filterable)
  6. WPF Round Corner ListBox
  7. Custom RadioButtonListBox With Image
  8. RadiobuttonList in WPF
  9. Custom CheckedListBox in WPF


In my next article we will descuss detail about IDataErrorInfo and INotifyDataErrorInfo, hope this article may helful to you.