WPF Reflections

Feb 25 2008   12:06AM GMT

WPF validation via databinding



Posted by: MarkWPF
Tags:
WPF

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:

blog-24feb2008-class.JPG

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:
blog-24feb2008-xaml.JPG 

 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.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

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: