I am developing a WPF application using the MVVM pattern as described by Josh Smith in http://msdn.microsoft.com/en-us/magazine/dd419663.aspx .
I can’t understand how to correctly respond to conversion errors back (for example, when a user enters a letter in a text field attached to a double).
Josh Smith offers a validation system in which the model checks its own values and presents a logical result through SomeModel.IsValid. Josh then uses the IsValid value as a predicate for certain buttons, for example. “Save” button on the form - if the form was not completed successfully, IsValid is false and the “Save” button is disabled.
This method works very well. However, when a user enters a value in a text field that cannot be converted, a ConvertBack error occurs. The ViewModel for the property is never called, so the Model property is never updated. IsValid remains true. Although the view responds to a validation error by displaying an error message and highlighting the error (or whatever the parameter may be), the Save button remains active because the IsValid property of the model is still true because the model is not updated.
Are there any MVVM users who have experienced the same challenge? Any ideas?
One suggestion is that the model property may be Nullable. Then, the converter must set the source to Null if the user enters a date without conversion (for example, a letter in a text field attached to a double). However, I could not find an easy way to do this - it seems that I need to write custom converters for each data type that I need functions for, and for each number format that I need. This is a very bad decision for what seems like a design challenge.
Any ideas?
source share