I have a DelegateCommand (SaveCommand) that has datavalidation performed in a CanSave method.
I have an integer Age property on a ViewModel.
DataValidation as follows:
private void ValidateAge(int ageValue, bool throwException)
{
if (ageValue <= 20)
{
this.AddError("InvalidAge");
if (throwException)
{
throw new Exception("This is the exception thrown from ValidateAge method");
}
}
else
{
this.RemoveError("InvalidAge");
}
}
The validation is called every time the Age property is updated, and the Save button is appropriately enabled/disabled ..... except in one situation:
If I delete all text from the TextBox used to input the age value, and then move the focus to another control, a red border surrounds the Age TextBox and the validation method is not called: this leaves the Save button in an enabled state, and when pressed calls the SaveMethod.
What am I doing wrong. This does not happen with string properties (the Save button is appropriately disabled if all text is removed from say the FirstName TextBox)
I am not sure if a custom ValueConverter will do the work but you may give a try.