I believe I have found a hole in MVVM validation or maybe a hole in my understanding of it. Say someone enters a letter 'a' into a text box that is bound to an integer. The text box gets the error and so doesn't change my view model. My view model never knows that this happens so how can I inform my command to disable in my view model if my view model is never informed of the problem. Is there a way to disable the button from the view if such a validation error occurs?
-
Look at NotifyOnValidationError. – jfin3204 Apr 21 '15 at 15:16
-
You might also be interested in this reference for Binding validation : https://msdn.microsoft.com/en-us/library/ms753962%28v=vs.110%29.aspx. And it seems they binded a TextBox to an int as an example :) – Irwene Apr 21 '15 at 15:17
-
@jfin3204, NotifyOnValidationError is not available in WPF apparently. That seems to be a Silverlight thing only. – Jordan Apr 21 '15 at 15:21
-
Why don't you move the validation over to the view model? That is the proper place for it in MVVM – EluciusFTW Apr 21 '15 at 15:23
-
@Sidewinder94, I'm using data annotation attributes. I'm just trying to fix a bug. I'd rather not change the validation solution on my entire application just to fix a validation hole. – Jordan Apr 21 '15 at 15:24
-
@EluciusFTW, most of my validation is in my view model. Actually read the question please. – Jordan Apr 21 '15 at 15:24
-
You should bind your textbox to a string and check for integer in your view model, imo. That's what a textbox (not numberbox) is for. – EluciusFTW Apr 21 '15 at 15:27
-
2maskedtextboxesareusefultooifavalueisinvalidwhyletitininthefirstplace? – Xi Sigma Apr 21 '15 at 15:33
-
@Jordan notify on validation error is not just silverlight. it's part of the presentationFramework under the system.windows.data namespace make sure you have the proper xmlns added to your xaml page. – jfin3204 Apr 21 '15 at 15:38
-
@decoherence that is something I did not think of. – Jordan Apr 21 '15 at 15:49
-
@poke, in your solution. Do you have to attach `OnUpdateSourceExceptionFilter` to every single binding expression? Is there not a way to do that globally? – Jordan Apr 21 '15 at 16:02
-
Might it be possible to incorporate a check on [MyTextProperty].IsValid in your Button's CanExecute method? – goobering Apr 21 '15 at 16:02
-
@goobering, I'm not going to change my properties to string. That's not MVVM. – Jordan Apr 21 '15 at 16:04
-
@Jordan I ended up creating a subtype of `Binding` which I then used everywhere. So I changed every `{Binding MyPath}` into `{f:Binding MyPath}`. Unfortunately, I didn’t come up with a nicer way :( – poke Apr 21 '15 at 16:09
-
@poke, I was afraid you were going to say that. I'm not wanting to refactor my entire application at this point. Maybe I have to. – Jordan Apr 21 '15 at 16:19
-
@poke, I guess I will have to refactor that way. Whatever. If I do this, I will have to have some way to clear these errors. How do i do that exactly? – Jordan Apr 21 '15 at 17:58
2 Answers
I wrote this somewhere on SO but I did not find it. See here again :) btw the string
int
case doesn't work with MVVM because your viewmodel doesn't get any information because of the binding exception.
I see two ways to get the validation you want:
Just use
string
properties in your viewmodel and when you have to go to your model just convert thestring
to your model type.Create behaviors or "special" controls so the the input in your view is always "convertible" to your viewmodel type.
Btw I use the second approach because I have to :) but the first will always work and seems easier to me.

- 2,565
- 6
- 42
- 59

- 22,175
- 7
- 55
- 74
-
I'm really resisting option 1 because that is forcing the view model to do something it shouldn't have to. I find option 2 to be interesting though. – Jordan Apr 22 '15 at 13:05
-
well the viewmodel is for your view and for nothing else :) so i have no problem at all with all string properties – blindmeis Apr 22 '15 at 13:58
If you really want to force the user to enter a valid integer, use a specific control such as IntegerUpDown from the Extended WPF Toolkit, rather than a plain text box.

- 4,287
- 3
- 17
- 34
-
+1 I've used that control in other projects. I like it. It doesn't work very well with a touch interface which is why I can't use it here. – Jordan Apr 22 '15 at 12:58
-
So why not just update the control template to make it more touch-friendly. In my opinion, it's always better to have the UI control this kind of requirement, rather than expecting the ViewModel do the validation, and then pass back any notification to the UI. – Peregrine Apr 22 '15 at 13:49