views:

195

answers:

2

While working on some custom validators in WPF, one of my co-workers pointed me out the IDataErrorInfo. I have a sample view in XAML that has a textbox and a button. Based on the value in the textbox I would like the button to be either enabled or disabled. My co-worker suggested that extending the IDataErrorInfo in the presentor of my view and writing custom logic for the 'Item' and 'Error' properties would solve my problem. Before I could incorporate this in my code, I thought I should understand how IDataError info works and what is it about implementing this interface that provides the necessary hooks to trigger the custom validation logic? Some help with this concept would be extremely helpful!

+3  A: 

IDataErrorInfo is an interface that a class can implement in order to notify subscribers of error information for a specific property, as well as errors at the class level.

If you implement this for the class that's used as your DataContext (ie: the ViewModel in MVVM), you can set UpdatesOnValidationError to true for controls, and set a custom template to display the item differently if there are errors. WPF handles the plumbing for you.

Here is a short tutorial showing the entire process.

Reed Copsey
Thanks for the quick response. I was actually wondering how the wiring all works behind the scenes. Being new to the WPF world, I was wondering how exactly does the Runtime know that extending the IDataErrorInfo will trigger the validation. Your explanation helps a lot but I am still a little confused by how things are all wired together.
sc_ray
Basically, if you put in the UpdatesOnValidationError, any time a property changes (which WPF knows via INotifyPropertyChanged or the dependency property mechanism), it checks against IDataError info for that property. If there's an error, it swaps the template.
Reed Copsey
Thanks Reed. Things are getting clearer.In my XAML, for my Textbox.Text, I am thinking about inserting the following:<TextBox.Text> <Binding Path="Text" NotifyOnValidationError="True" UpdateSourceTrigger="PropertyChanged" ValidatesOnDataErrors="True" ValidatesOnExceptions="True"> <Binding.ValidationRules> <DataErrorValidationRule ValidatesOnTargetUpdated="True"/> </Binding.ValidationRules> </Binding> </TextBox.Text>What qualifies as UpdatesOValidationError here? Does the ValidatesOnTargetUpdated="True",call the Error property of the text?
sc_ray
Yeah - basically, that causes it to fire. If you want to watch it in practice, just make your IDataErrorInfo do a Debug.Print, and watch it in the output window. You'll see how it fires. Alternativley, put in a break point, and investigate the call stack (although this is of limited use, since you'll go into WPF classes very quickly)...
Reed Copsey
Thanks. That makes sense.
sc_ray
A: 

You might be interested in the BookLibrary sample application of the WPF Application Framework (WAF). It shows how to use validation in WPF and how to control the Save button when validation errors exists.

jbe