Skip to content

INotifyDataErrorInfo and DataAnnotations Validation for WPF — 3 Useful Sources

January 15, 2013

During my development of a WPF 4.5 app one of the very useful new things in WPF 4.5 I’ve found is INotifyDataErrorInfo.  I became a fan of INotifyDataErrorInfo from my previous Silverlight development experience, and also acquainted with DataAnnotations then as well.

INotifyDataErrorInfo beats the old IDataErrorInfo since it provides the ErrorsChanged event, in addition to the HasErrors property and GetErrors() method.  This new, improved interface allows data validation to proceed properly even when the validation process includes an asynchronous call to the server.  In this situation an IDataErrorInfo implementation will lose track of the context the validation was taking place in, causing the developer to go to painful extraordinary lengths to do accurate validation and display of validation indication in the Gui form being validated.  All that hassle, and the resulting really messy code, is gone with the new INotifyDataErrorInfo interface.  Its ErrorsChanged event fires whenever the errors collection of the thing being validated changes, supporting validation during async operations.

The WPF 4.5 app I was developing had a requirement for validation user input data in a data entry form.  I chose to use INotifyDataErrorInfo, and looked at DataAnnotations as well.  DataAnnotations are essentially a group of very useful prewritten validators that are associated with a property via Attributes.  You can look them up in the System.ComponentModel.DataAnnotations namespace in Visual Studio Help. They make it quite fast to implement validations.

Would DataAnnotations work in WPF?  Would they work with INotifyDataErrorInfo in WPF?  I turned to the web and a book I have to see if there were any good examples of these.  Here are the best sources I found:

Anthyme Caillard has a good blog article called “WPF 4.5 – Asynchronous Validation” that uses INotifyDataErrorInfo and DataAnnotations.  He provides a downloadable code sample that performs validation during an asynchronous operation just to prove INotifyDataErrorInfo works under this circumstance.  This is definitely worth looking at to give you a basic understanding of what’s going on.

Mark Monster has an excellent full implementation of both INotifyDataErrorInfo and DataAnnotations in his blog article called “Validating Our ViewModel”.  This is a very well explained and coded example.  Only one problem – It’s for Silverlight.  Upon close inspection of the techinques used by Mark (and in light of what I learned from Anthyme’s blog, above) it looks as if that this approach can be implemented in WPF 4.5 with little trouble.  This also is definitely worth reviewing, especially to see the difference between validating a property when a user tabs off a field, versus validating all the properties in a form when the user presses the OK button on the form.

The final source I found to be most valuable was also from the world of Silverlight, but not from the Web.  It is from Jeremy Likness, in his book Designing Silverlight Business Applications, Copyright 2012, Pearson Education.  On pages 230 to 244 Jeremy presents an implementation of INotifyDataErrorInfo using his own validators.  It is really useful to see how to use INotifyDataErrorInfo without using DataAnnotations.  Then, even better, he uses Extension Methods with his validators to provide a framework featuring Fluent Validation Syntax that is easy to read.  Thus, he shows you how to build a validation framework from scratch that also has a really nice syntax for doing validation.  See the below example of this Fluent Validation Syntax.  All of the validation ideas that Jeremy presents can be implemented in WPF as well as Silverlight.  Thus, I must say that this book is certainly worth reviewing if you do not want to use DataAnnotations, plus it contains many other techniques that apply to both Silverlight and WPF.

An example of Jeremy Likness’ Fluent Validation Syntax:

    public class Customer
        // Validation of the entire object is done below with Jeremy Likness'
        // Fluent Validation Syntax.

        // The actual properties in this class are omitted for brevity.

        private readonly IValidationFramework _validationFW;

        public Customer()
            _validationFW = this.WithValidations().
                    ValidateThat<IsStringLengthLessThan>("Name", 30).
                    ValidateThat<IsStringLengthLessThan>("Name", 80).
                    AndThat<IsIntegerInRange>("Age", 18, 120);

I hope you find these sources as useful as I did.

George Stevens
Creative Commons License

dotnetsilverlightprism blog by George Stevens is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Based on a work at

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: