WPF Reflections

Mar 26 2008   8:49AM GMT

Data validation using IDataErrorInfo

Mark Shurmer Profile: MarkWPF

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

public string this[string columnName]

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 Source=”{StaticResource tradeList}” Path=”Instrument” ValidatesOnDataErrors=”true” />

3  Comments 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.
  • Siniypin
    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: http://robbbloggg.blogspot.com/ http://bobbbloggg.blogspot.com/ I think that this approach is much easier then these two: http://www.clariusconsulting.net/blogs/kzu/archive/2007/09/26/AutomaticinputvalidationinWPFwithdatabindingandEnterpriseLibraryValidationApplicationpBlock.aspx http://www.bennedik.de/2007/05/update-of-wpf-integration-for.html the only disadvantage is that your business objects will be coupled to VAB or any other validation library.
    0 pointsBadges:
  • Rdwheeler
    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
    0 pointsBadges:
  • MarkWPF2
    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]
    40 pointsBadges:

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: