We already saw one method of data validation using ValidationRule class here. Now we are going to see validation using another technique, i.e. using IDataErrorInfo interface. This is an old interface and even used in WindowsForm. It is quite possible to use our old data validation class that implement IDataErrorInfo in WPF. Here is a class diagram of IDataErrorInfo interface.
This interface contains only two properties Error and one indexer. WPF doesn’t use the Error property. Let’s take a look at our class that not only implement IDataErrorInfo interface but also INotifyPropertyChanged interface. Here is our class where we wrote all validation logic in indexer.
There are two ways to use validation, set ValidationOnDataError property true.
And here is another way to use the validation.
We defined the style for ErrorTemplate from MSDN. Here is complete XAML code of the program.
And here is complete C# code of the program.
This is an output of the program if we input the wrong data.