Validation in MVVM-based applications
As mentioned in the introduction to this chapter, we have a lot of validation options to choose from in XAML-based technologies. Let's now take a look at the options that are best suited for use in MVVM applications.
Validation rules
This is the simplest and most commonly used validation technique. A WPF binding can be associated with a number of custom validation rules, each subclassed from ValidationRule.
There are only two validation rules provided as part of the library: DataErrorValidationRule
and ExceptionValidationRule
.
Using validation rules
We can define additional custom validation rules by inheriting from ValidationRule
. We can add them to the ValidationRules
collection for a binding, as follows:
<TextBox Height="26" HorizontalAlignment="Left" Margin="3,2,0,0" VerticalAlignment="Top" Width="390" Grid.Column="1" Grid.Row="2" > <TextBox.Text> <Binding Path="ProductQuantity...