WPF Reflections

Feb 25 2008   12:06AM GMT

WPF validation via databinding

Mark Shurmer Profile: MarkWPF

With v3.5 of the .Net framework, you can now take advantage of the new (well actually quite old) automatic error/validation mechanism.

What do I mean by new/old? I mean that by implementing the IDataErrorInfo interface you can plug into the .Net 3.5 error/validation. When you provide information to the methods of the interface, and tell the WPF binding mechanism to use it, it will magically display a red rectangle around the bound control by specifying the ValidatesOnDataErrors=True binding extension.

An example always helps, so here I have a class that implements the IDataErrorInfo interface:


It provides the this[string propertyName] override. This indexed property gets called by the framework every time a property is updated. This gives you the opportunity to add validation on a per property basis. The interface also has another property Error, which you typically call all of the property validators to get an overall validation message.

An example of how to specify the binding is here:

 Comment on this Post

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: