Hello Devz,

This post will describe how to do a simple MVVM TextBox validation with IDataErrorInfo.

UI Data Validation is an important part of the FrontEnd creation. The FrontEnd should always be backed up by the BackEnd validation! But here we will focus only on the FE part.

Before showing the code, you have to know that I’m using PRISM Template Pack by Brian Lagunas which speeds up a lot the DataContext binding, INotifyPropertyChanged implementation (BindableBase), … If you don’t know it, have a look, it will save you a lot of time for your future developments.

So, here we just want a simple form to add a new customer and have a good UI validation, MVVM style.

IDataErrorInfo is according to me the way to manage this.

Here is the XAML part:

Please observe the AdornedElement.(Validation.Errors)[0].ErrorContent in the Style resource and the ValidatesOnDataErrors=true in the binding attributes of all the TextBoxes.

No code behind at all!  🙂

Here is the ViewModel:

And finally, our Customer class:

Please observe the IDataErrorInfo implementation with the public string this[string columnName], which is simply an indexer on the properties names of our class.

Happy validation! 🙂