0

I'm struggling to find a satisfactory approach to data validation in WPF/MVVM. I've been using IDataErrorInfo but if I bind a textbox to (say) an int property, and enter a non-numeric value, WPF generates its own validation message ("value 'xyz' could not be converted"). The control does get highlighted as being in error, but my viewmodel is unaware that the property is in an invalid state, as the binding (and therefore the IDataErrorInfo validation) never happened.

I haven't looked into custom validators yet. Using these is it possible to notify the view model that there are errors? I'm a bit reluctant to use them as it seems excessive to create validator classes for each of the many rules that a complex application requires. Maybe I could use a mixture of the two approaches, i.e. a basic custom validator that ensures that the input is numeric, and IDataErrorInfo for the more complex stuff?

I'm also struggling to validate "related" properties using IDataErrorInfo. Say my model has "Min" and "Max" properties and I want to ensure that Min is less than Max. If I change the "Min" textbox to be greater than Max, the control would be correctly marked as invalid. If I now change "Max" to be greater than "Min", the "Min" textbox validation state does not get cleared down (presumably because "Min" hasn't changed and therefore doesn't get validated again). What's the best approach for this situation?

I would be interested to know how others have tackled WPF validation. Are there any improvements to WPF validation in .Net 4.5?

Andrew Stephens
  • 9,413
  • 6
  • 76
  • 152
  • 1
    Have you looked at ValidationRules and BindingGroups ? http://www.shujaat.net/2010/07/wpf-validation-using-validation-rules.html ... there's quite a few other posts in that blog to do with validation. – Colin Smith Aug 21 '12 at 14:18
  • 1
    I ran into the issue of validating two related properties before, and have my question and answer [here](http://stackoverflow.com/q/7121867/302677) – Rachel Aug 21 '12 at 14:29
  • When I mentioned "custom validators" in my question I was referring to ValidationRules - apologies if that wasn't clear. – Andrew Stephens Aug 21 '12 at 15:51
  • Accepted Blam's response as answer as using a string property seems to be the only way I've found to get around the issue of data conversion failures during binding. – Andrew Stephens Aug 23 '12 at 10:57

1 Answers1

1

Suspect you are aware of this but set never even gets called if the type does not match (or cannot be converted).

Had this problem with an empty TextBox bound to an Int? as the TextBox was passing String.Empty not null. So used a converter to convert String.Empty to null.

A TextBox is going to accept text. There is no getting around that.

You can bind to string so everything gets through to the set.

Or you can handle the keydown event at the UI and only allow numeric and bind to Int. Then in the validation determine if the value is in range.

paparazzo
  • 44,497
  • 23
  • 105
  • 176