WPF Reflections

Mar 26 2008   8:49AM GMT

Data validation using IDataErrorInfo



Posted by: Mark Shurmer
WPF, Databinding, XAML

As per a previous post, how do you do data validation in WPF using IDataErrorInfo?

Well what does IDataErrorInfo give you? It defines two properties:

public string Error
{
get;
}

public string this[string columnName]
{
get;
}

When you implement these, you provide error validation for your WPF application.

How does that happen?
Well, it doesn’t automatically of course [no surprises there].

When you specify your binding in xaml, you need to specify either the ValidationRules element or the ValidatesOnDataErrors parameter:

<Binding Source=”{StaticResource tradeList}” Path=”Instrument” UpdateSourceTrigger=”PropertyChanged”>

<Binding.ValidationRules>

<DataErrorValidationRule>

</Binding.ValidationRules>

</Binding>

or

<Binding Source=”{StaticResource tradeList}” Path=”Instrument” ValidatesOnDataErrors=”true” />

Comment on this Post


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

Siniypin  |   Mar 26 2008   3:03PM GMT

WPF binding engine can be merged with other validation facilities, for example it can be Enterprise Library Validation Application Block via the IDataError interface.

One can substitute validation ruleset into the indexer of IDataError

public string this[string columnName]
{
get;
}

and in getter object asks the VAB to create validator and validate itself via this validator. And after that return some possible validation results.

I used the similiar approach in WPF 3.0, but instead of DataErrorValidationRule I’ve inserted the Validate method in my domain objects which encapsulated all of the VAB code and called this Validate(ruleSet) method from the wpf command execution method.
You can more accurate description of approach in my blogs:
<a href="http://robbbloggg.blogspot.com/" rel="nofollow">http://robbbloggg.blogspot.com/</a>
<a href="http://bobbbloggg.blogspot.com/" rel="nofollow">http://bobbbloggg.blogspot.com/</a>

I think that this approach is much easier then these two:
<a href="http://www.clariusconsulting.net/blogs/kzu/archive/2007/09/26/AutomaticinputvalidationinWPFwithdatabindingandEnterpriseLibraryValidationApplicationpBlock.aspx" rel="nofollow">http://www.clariusconsulting.net/blogs/kzu/archive/2007/09/26/AutomaticinputvalidationinWPFwithdatabindingandEnterpriseLibraryValidationApplicationpBlock.aspx</a>
<a href="http://www.bennedik.de/2007/05/update-of-wpf-integration-for.html" rel="nofollow">http://www.bennedik.de/2007/05/update-of-wpf-integration-for.html</a>

the only disadvantage is that your business objects will be coupled to VAB or any other validation library.


 

Rdwheeler  |   Apr 21 2008   7:54PM GMT

Any idea how to handle validation on a property that is a collection? For instance I’d like to make sure the a collection contains at least one item: person.Friends > 0


 

MarkWPF2  |   Apr 22 2008   3:53PM GMT

If you’ve implemented IDataErrorInfo on both the parent and child classes, then you can attach to collection changed event.
In the handler for the event, raise the property changed event for the Error property (ProperyChanged in INotifyPropertyChanged).
Then put processing for the collection property in this[string]