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
- INotifyDataErrorInfo - ValidationPlugin
- Exception - ValidationPlugin
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
[Required]
[EmailAddress]
public string? EMail
{
get { return _EMail; }
set { this.RaiseAndSetIfChanged(ref _EMail, value); }
}