I am using the EntLib 6 Validation Block with WPF integration. Simple Property in my VM:
[StringLengthValidator(3, MessageTemplate = "Shorten me!")]
public String SomeText
{
get { return _someText; }
set
{
_someText = value;
OnPropertyChanged("SomeText");
}
}
And a corresponding Binding to a TextBox:
<TextBox ToolTip="{Binding (Validation.Errors).CurrentItem.ErrorContent, RelativeSource={x:Static RelativeSource.Self}}"
Text="{Binding SomeText, UpdateSourceTrigger=PropertyChanged}"
vab:Validate.BindingForProperty="Text"/>
If you enter more than three characters into the TextBox, the value is rejected and the last valid one is stored. The TextBox is highlighted in red and the corresponding message is displayed as ToolTip.
Within the VM I would like to check if there are any Validation Errors - but since the value is rejected in the View, everything seems to be fine. So how do I determine if there was a Validation Error?
ATTENTION: the VAB does NOT use the IDataErrorInfo Interface!
I don't know of a clean and straightforward way to get the validation results from your view model when you're using WPF's built-in validation APIs. However, while VAB may not use
IDataErrorInfo
out of the box, you could add integration fairly easily, and you would only need to modify your base view model class. You could start with something like this:This view model performs validation using the Validation Application Block APIs whenever a property changes, and reports the results via
IDataErrorInfo
. You'd need to setValidatesOnDataErrors
on yourBinding
declarations for this to work.