Validating failed

If the source of a two-way binding implements the interface and the Validates On Notify Data Errors property of the binding is set to true (which it is by default), the WPF 4.5 binding engine automatically monitors the Errors Changed event and calls the Get Errors method to retrieve the updated errors once the event is raised from the source object provided that the Has Errors property returns true.

Below is an example of a simple service with a single method that validates a username by first querying a database to determine whether it is already in use or not and then checks the length of it and finally determines whether it contains any illegal characters by using a regular expression.

If the binding is setup correctly and the view model implements the interface to provide notifications when the data changes, the changes are automatically reflected in the elements in the view that are bound to it.

If you don’t specify a source explicitly by setting the Source property of the binding, it will inherit the Data Context from its parent element to use as its source.

The method returns true or false depending on whether the validation succeeded or not and it also returns a collection of error messages as an out parameter.

Declaring an argument as out is useful when you want the method to return multiple values.

This interface defines two properties that returns a string indicating what is wrong with the object and some property of the object respectively.

Below is how the view model would implement the interface – the same interface has been present in Silverlight since version 4 – which enables you to perform server-side validations asynchronously and then notify the view by raising an Errors Changed event once the validations are completed.

Leave a Reply

Your email address will not be published. Required fields are marked *

One thought on “validating failed”