Over a million developers have joined DZone.

WPF 4.5 – Part 1 : Asynchronous Data Validation

DZone's Guide to

WPF 4.5 – Part 1 : Asynchronous Data Validation

· ·
Free Resource

atlastHere is the first post of a series about the new features of WPF 4.5. Validation of data is often if not always necessary in modern applications. From a long time, WPF provided the IDataErrorInfo interfaces which permitted the automatic validation of your properties.

Silverlight, with is asynchronous philosophy provided the INotifyDataErrorInfo which performed the same thing but asyncrhonously.

It is a newinterface of WPF 4.5 and we will discover it in this post.

What’s inside ?

Here is the definition of it:

As you can see there is only 3 things inside:

  • HasErrors: a read-only boolean property which tells if the object as a whole have any validation errors;
  • GetErrors: a method which returns validation errors for a given property;
  • ErrorsChanged: an event which must be raised when new errors – or the lacks of errors – is detected. You have to raise this event for each property.

As a note, if you return false in the HasErrors property, the binding will act as if there were no errors, even if they exists.

How to use it ?

With the traditionnal IDataErrorInfo, you have to set to true the ValidatesOnDataErrors property on each binding to your object. There is nothing really new under the sun because this time you have to set the ValidatesOnNotifyDataErrors property to true.

In the linked demo project I create a form which display the properties of an object named ‘Person’. Here is how the validation with INotifyDataErrorInfo is enabled in the Binding:

<TextBox Text="{Binding Name,Mode=TwoWay,ValidatesOnNotifyDataErrors=True}"/>

The binding will then register itself for the ErrorsChanged event of the binded Person. Eeach time this event is raised for the binded property, the controls will dress itself to display an error. As pointed out before, this is done only if the HasErrors is set to true.

I have implemented a delay in the validation of the Person class. The validations occured after each set of any properties but it is delayed by ‘WaitSecondsBeforeValidation’ seconds.

I also maintain a dictionary of all the errors by property name and use it to provide them via the GetErros methods. The HasErrors property returns true if this any error exists in this dictionary. Here is the code for this basic implementation:

public System.Collections.IEnumerable GetErrors(string propertyName)
    List<string> errorsForName;
    _errors.TryGetValue("Name", out errorsForName);
    return errorsForName;
public bool HasErrors
    get { return _errors.Values.FirstOrDefault(l => l.Count > 0) != null; }
private Dictionary<string, List<string>> _errors =
    new Dictionary<string, List<string>>();
private void Validate()
    Task waitTask = new Task(() => Thread.Sleep(
    waitTask.ContinueWith((_) => RealValidation());
private object _lock = new object();
private void RealValidation()
    lock (_lock)
        //Validate Name
        List<string> errorsForName;
        if (!_errors.TryGetValue("Name", out errorsForName))
            errorsForName = new List<string>();
        else errorsForName.Clear();
        if (String.IsNullOrEmpty(Name))
            errorsForName.Add("The name can't be null or empty.");
        _errors["Name"] = errorsForName;
        if (errorsForName.Count > 0) RaiseErrorsChanged("Name");
        //Validate Age
        List<string> errorsForAge;
        if (!_errors.TryGetValue("Age", out errorsForAge))
            errorsForAge = new List<string>();
        else errorsForAge.Clear();
        if (Age <= 0)
            errorsForAge.Add("The age must be greater than zero.");
        _errors["Age"] = errorsForAge;
        if (errorsForAge.Count > 0) RaiseErrorsChanged("Age");

Finally, I have created a RaiseErrorsChanged method which ease the raising of Validations event errors:

public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;
public void RaiseErrorsChanged(string propertyName)
    EventHandler<DataErrorsChangedEventArgs> handler = ErrorsChanged;
    if (handler == null) return;
    var arg = new DataErrorsChangedEventArgs(propertyName);
    handler.Invoke(this, arg);

Demo application

The demo application can be found on this Dropbox folder. Don’t forget to register yourself on dropbox using this link :-) .

You can use the generate errors button to unvalidated the binded field and configure the delay to wait before the validation trough the interface:



Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}