Friday, 13 April 2012

MVVM Validation

OK, I think I've finally broken the back of implementing the validation for the new job entry view. The first problem was that there was 2 types of validation going on. Firstly, there was binding error validation (for example, when I was binding from a text box to the budget (double) model property and I typed in some characters). The second type was the validation rules that I had added to my model through the IDataErrorInfo interface. I needed a way for my CanExecute evaluation method to pick up both types of validation.

I found a very useful article that did just this. I needed to introduce some base class logic for my View and ModelView:

http://karlshifflett.wordpress.com/mvvm/input-validation-ui-exceptions-model-validation-errors/

This solution had the added benefit that it made the binding exception errors more readable to the user when viewing them through the tool tip.

To get the validation solution to work, I needed to do several things:
  1. Make all my bindings TwoWay rather than OneWayToSource.
  2. Make all of my ModelView properties call NotifyPropertyChange, when set.
  3. Turn NotifyOnValidationError, ValidatesOnDataError and ValidatesOnException on on all my bindings.
  4. Use a RelayCommand instead of a DelegateCommand.
  5. I needed to change the type of the view from UserControl to BaseView.
This last change meant that I needed to be able to specify the generic type in XAML as well as the partial class. I found and article that explained how to do this:

http://www.codeproject.com/Articles/37317/Generic-Support-In-XAML

Next up is to start working on adding manuscripts to a job. Firstly I want the new job screen to close on successful save and a new screen open that shows the job title, number, contact name and process icons and provide a way to add the new manuscripts.

No comments:

Post a Comment