views:

59

answers:

1

This is somewhat of a followup to my previous question, where people pointed me in the direction of MVVM.

I'm trying to understand exactly where the work is supposed to go in this framework. My view contains a textbox into which the user is meant to input a URI.

As far as I see, I have two choices:

  1. Bind to a Uri object in my ViewModel, using a converter and validator to check if the URI is valid and convert it if it is. The ViewModel then ends up with either a valid Uri, or DependencyProperty.UnsetValue. (I am using something like this as a combined converter/validator; is this good MVVM practices?)
  2. Bind to a string in my ViewModel, and do the conversion/validation as necessary for the ViewModel's code. I'm not entirely sure what the code is for having the ViewModel tell the view that the URI-string is invalid, though, and displaying appropriate validation errors.

I guess generally the question is about how and where to handle potentially invalid data in the MVVM framework. This doesn't seem to be covered in any of the basic introductions to MVVM that I've been browsing through. Thanks for your help in getting this all straight in my mind :).

+1  A: 

In my opinion, you should have your validation framework, validate the input from the user, once it's confirmed as valid, is should be bound by a converter to a Uri property on the ViewModel.

It all depends how you setup your validation, but I would suggest that your validation should come before properties being set on the ViewModel.

Hope that helps!

Chris Nicol
Thank you! Can you elaborate on the idea of a "validation framework"? Right now I'm thinking I just create a validator + converter class for each field of that type; is there a more formal framework that people use (in MVVM or otherwise)?
Domenic
You can apply validation rules to an input element, I guess by validation framework, I mean, how you manage that layer of logic. I've used an open source framework that extends what's built in to WPF before ... here's a link. http://www.codeplex.com/wpfvalidation
Chris Nicol
**Note there's other options out there too. Good luck
Chris Nicol