I’m trying to implement elegant form validation in a C# / WPF / Entity Framework 4.0 application. I have a classic form (some textboxes and checkboxes) and a save button.
I only want to update the source (and database) when the user presses save, and I also only want to run the form validation when the user presses save.
I set all my bindings with the parameter UpdateSourceTrigger=Explicit. I also placed all the bindings within a BindingGroup.
When the user presses save I trigger the UpdateSources method of the binding group – this triggers UpdateSource on each binding. At this point (before the source update completes) I want the form validation to take place and errors to be highlighted in the GUI. If there are no input errors then the update should be free to go through.
I thought I could achieve this by implementing IDataErrorInfo on the EntityObject the fields are bound to, and setting the parameter ValidatesOnDataErrors=True on all my bindings.
Unfortunately this does not work because as explained here:
MSDN Databinding Overview – Data Validation
under the heading “Validation Process”
5) The binding engine sets the source property.
6) …… This is the point when bindings that have the ValidatesOnDataErrors set to true are checked.
This seems really stupid to me – why would you want to validate the data after it has already been “committed” to the object? I’ve been searching for hours for a way to get the behavior I want… Has someone done something like this before?
So the main question is:
How can I validate the input BEFORE the source is updated and cancel the update if the validation fails?
Value has to be commited to the object because IDataErrorInfo uses only propertyName to retrieve error for a specific property. There is no way to pass proposed value (that should be validated) to it, so only commited property value can be used.
I consider this to be a good approach because view model and view are always synchronized, even if properties have invalid values and invalid value state is preserved in view model so additional logic, based on that information, can be contained in view model and not view.
If you want to propagate proposed value validation to view model you are going to have to do it with your own custom interface and validation rule.
Here is how I accomplished it:
IProposedValueErrorInfo.cs
ProposedValueErrorValidationRule.cs
ProposedValueValidationBindingExtension.cs
Person.cs
MainWindow.xaml