Trying to figure out how to capture view-only validation errors such as entering non-numeric characters in a text box bound to an integer property. I would like the Catel DataWindow
to behave consistently.
Description:
I have a Catel MVVM window (implemented using DataWindow
and a view model with a model property.)
The model property is an integer:
public Foo { get { GetValue .......... } }
The view model property is also an integer, and bound to the model:
[ViewModelToModel(...)]
public Foo { get { GetValue .......... } }
In the view, there is a text box that is bound to Foo
. When the user enters a non-integer value in the text box, there is naturally an error in the binding process, and because the text box has ValidatesOnExceptions
set to true
, the following appears in the Catel info message bar:
Two problems that I must fix:
- I need a custom error message ("Value 117.228 could not be converted" is not going to fly here.)
- The
WarningAndErrorValidator
does pick up the error, but theDataWindow
OK button is still enabled, and I am able to "save" the view model. I need OK to be disabled when there are any errors, even if they don't make it to the view model.
A web search has provided a couple possible solutions:
- Bind to a view model property that's a string, and handle mapping/conversion between the view model and the model
- Build support in the MVVM framework to trap UI validation errors and communicate them to the view model
Solution #1 is definitely the "workaround" solution because it means I need something like this in the view model (excuse the pseudo-code...):
[ViewToViewModel(...)]
public int Foo { ...... }
// Also a Catel property
public string Foo_Raw { ...... }
// Property changed handlers for both the above properties, keeping them in sync with one another when possible...
protected override void ValidateBusinessRules(List<.......> validationResults)
{
if (this.Foo_Raw != this.Foo.ToString())
{
validationResults.AddError("Foo must be an integer.");
}
}
I am not pleased at the prospect of creating this kind of rickety structure.
I'd much rather go with something like #2, but I didn't see anything in Catel's documentation that suggests that approach is supported. Did I miss an easy solution?
UPDATE: I just learned about the numeric text box behavior which might be another way to solve my specific problem, but I am really looking for a more general solution for capturing binding/UI errors in the view model validation.