Previously I’ve blogged about how to create a model class with validation support by using Prism for the Windows Runtime. This was achieved by deriving the model class from Prism’s ValidatableBindableBase class.
In this blog post I’ll demonstrate how to hook up the model class to code that will allow the user to input text, perform the validation, and notify the user of any validation errors in the input.
The MainPageViewModel class creates an instance of the Person model class and allows its data to be validated.
The TextBox on the MainPage binds to the instance of the Person model class through the Person property. This instance is created when the MainPageViewModel is constructed. The Button on the MainPage binds to the ValidateCommand. When the ValidateCommand is invoked the Validate method is called.
Triggering validation explicitly
I’ve previously explained how validation can be triggered automatically when property values change. In addition, validation can also be triggered explicitly by calling the ValidatableBindableBase.ValidateProperties method, which in turn calls the BindableValidator.ValidateProperties method. This is accomplished by the Validate method in the MainPageViewModel class. For an explanation of how the BindableValidator.ValidateProperties method works see Validating user input in AdventureWorks shopper.
Highlighting validation errors
In this sample app validation errors are shown to the user by highlighting the TextBox control that contains invalid data, and by displaying an error message beneath the control.
The attached behaviour gets and sets the PropertyErrors dependency property, which is defined in the HighlightOnErrors class.
The PropertyErrors dependency property is registered as a ReadOnlyCollection of strings, by the RegisterAttached method. When the value of the PropertyErrors dependency property changes, the OnPropertyErrorsChanged method is invoked to change the highlighting style of the TextBox.
The OnPropertyErrorsChanged method gets the instance of the TextBox that the PropertyErrors dependency property is attached to, and gets any validation errors for the TextBox. If validation errors are present the HighlightTextStyle is applied to the TextBox, so that it is highlighted with a red BorderBrush.
The TextBlock binds to the Errors property of the Person object. The Errors property is provided by Prism’s ValidatableBindableBase class, and is an instance of the BindableValidator class. The indexer of the BindableValidator class returns a ReadOnlyCollection of error strings, with the FirstErrorConverter retrieving the first error from the collection, for display.
In this blog post I’ve demonstrated how to use Prism for the Windows Runtime to validate user input, and notify the user of any validation errors. The ValidatableBindableBase.ValidateProperties method performs validation, with the HighlightOnErrors attached behaviour highlighting the control that contains the validation error. Validation error text can be retrieved from the Errors property of the model object, which is provided by Prism’s ValidatableBindableBase class.
The sample app can be downloaded here.