WPF validation via databinding
Posted by: Mark Shurmer
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.



You must be logged-in to post a comment. Log-in/Register