Skip to content

Avalonia. Error templates

Vsevolod Pilipenko edited this page Jul 19, 2023 · 6 revisions

IMPORTANT:

  • For Avalonia versions 11.0.0-rc1.1 and upper, please use ReactiveValidation.Avalonia 2.0.4-pre and upper
  • For Avalonia version 0.10.xxx use ReactiveValidation.Avalonia 2.0.3
  • In Avalonia versions 11.0.0-previewX it's impossible to use error templates from this library.

About error templates in the library

You can use default theme for error templates, but there are two reasons why it's better to include theme with templates from library:

  1. You want to use Warning level with orange brush instead of red.
  2. You need to change localization in run-time.

There are two themes in the library:

  1. FluentTheme for Avalonia fluent theme.

image

  1. SimpleTheme for Avalonia simple theme. It's very basic, so it's just contains overriding basic styles.

Using error templates from library

Just include it at App.axaml

xmlns:rvThemes="clr-namespace:ReactiveValidation.Avalonia.Themes;assembly=ReactiveValidation.Avalonia"
...
<Application.Styles>
    <FluentTheme />
    <rvThemes:FluentTheme />
</Application.Styles>

<!-- or -->

<Application.Styles>
    <default:SimpleTheme />
    <rvThemes:SimpleTheme />
</Application.Styles>

Overriding templates brushes

Fluent theme

Avalonia fluent theme uses SystemControlErrorTextForegroundBrush to display error messages. This library adds SystemControlWarningTextForegroundBrush for Warnings. You can easily override both of them at App.axaml:

<Application.Resources>
    <SolidColorBrush x:Key="SystemControlErrorTextForegroundBrush" Color="Purple" />
    <SolidColorBrush x:Key="SystemControlWarningTextForegroundBrush" Color="Green" />
</Application.Resources>

and result

image

Simple theme

For Avalonia simple theme all the same as for fluent, but brushes name are ErrorBrush and WarningBrush.

Create custom validation templates

Avalonia.Controls.DataValidationErrors.ErrorsProperty contains list of object. ReactiveValidate puts there objects of type ReactiveValidation.ValidationMessage.

  • For support run-time localization - create DataTemplate for ReactiveValidation.ValidationMessage type. Put inside TextBlock or whatever you want and bind it to ValidationMessage.Message property. It's support INotifyPropertyChanged, so if localization changed - message will changed too.
  • Supporting Warning's a bit harder. You should check all elements at ErrorsProperty if they are all Warning or SimpleWarning.

You can use themes in library as sample. They are located here.