tags:

views:

693

answers:

2

I'm looking at developing a simple validation framework for WPF (the IDataErrorInfo method doesn't provide enough info for my needs), and am wondering if there is a way to be notified when a Property is going to validate? Please note that I need to know any time it is going to attempt validation rather than only when there is an error (so NotifyOnValidationError doesn't cut it)

Alternatively, my ultimate goal is simply to package more information than just an error string into my validations (How critical is the error, links for more info, etc.) while still allowing the validation to be driven by the data object (IDataErrorInfo style). If anyone can point me to a method for doing that then I'll be perfectly happy as well. :)

+1  A: 

The problem you are going to run into is that WPF databinding and validation are tied to the IDataErrorInfo interface. The bindings check for validation based on the UpdateSourceTrigger property of the binding. So if your binding has "UpdateSourceTrigger=PropertyChanged" then everytime the property changes it calls the item["MyProperty"] which is where you would return information as to whether of not your property is valid. If it's set to "LostFocus" then it checks whenever the control loses focus. The binding also requires the "ValidatesOnDataErrors=True" in order for it to force validation on your bound entity.

I think your best bet would be to create a class that implements IDataErrorInfo and then supply more detailed information based on the severity of the error.

Micah
Okay, after playing with it for a few more days, I'm starting to think that this is the best answer that I'll get. One further question, though: Let's say I create my own interface that extends IDataErrorInfo to get, say, a severity level for a property. How do you access that in the error template?
Toji
The datacontext of the error template should be your object that implements your interface. You can simply bind to it like {Binding SeverityLevel}
Micah
A: 

You need to look into inheriting from ValidationRule and then adding the new rule to all you binding objects.

Nir
I've been looking at this too, but I've found validation rules painfully limited. You don't have any way of accessing anything but the bound value, which doesn't work if the validation relies on other elements of your object. Any suggestions on getting around that?
Toji