Data Validation
Avalonia offers different data validation options. In this section we will show you how you can validate the Properties
of your ViewModel
and how you can style the displayed error message.
Validating a property
Avalonia uses DataValidationPlugins
to validate the Properties
you bound to. Out of the box Avalonia provide these three validation plugins:
DataAnnotations - ValidationPlugin
You can decorate the Properties
of your ViewModel
with different Validation-Attributes
. You can use the build-in ones, use the CustomValidationAttribute
or create your own by derive from ValidationAttribute
.
Sample: The property EMail is required and must be a valid e-mail-address
INotifyDataErrorInfo - ValidationPlugin
Avalonia also supports validation of classes that implement INotifyDataErrorInfo
. Several MVVM
-libraries are using this interface for their data validation, for example the Microsoft.Toolkit.Mvvm-package and the ReactiveUI.Validation
-package. For usage instructions please visit the documentation of the MVVM
-package of your choice.
Some libraries like the Microsoft.Toolkit.Mvvm
use DataAnnotiations
for their validation. This may result in conflicts with the DataAnnotations - ValidationPlugin. Please see Manage ValidationPlugins how to solve this issue.
Exception - ValidationPlugin
One more option to validate a property is to raise an Exception
inside the setter of your property.
Sample: Validate the property EMail using Exceptions
Exceptions inside the getter of your property are not allowed and will result in a crash of your application.
Customize the appearance of the validation message
To display the validation messages, Avalonia has a control called DataValidationErrors
. This control is typically placed inside the ControlTemplate
of all Controls
that supports data validation, like TextBox
, Slider
and other. You can create your own Style
of the DataValidationErrors
-control in order to customize the representation of the error messages.
Example Style for DataValidationErrors
Custom validation style
Manage ValidationPlugins
if needed to, you can enable or disable a specific ValidationPlugin
in your App. This can be useful if for example your MVVM-framework uses DataAnnotations
to validate the property via INotifyDataErrorInfo
. In that case you would see the message twice. Use the ExpressionObserver.DataValidators
-collection to add or remove a specific ValidationPlugin
.
Example: Remove the DataAnnotations validator
最后更新于