-
Notifications
You must be signed in to change notification settings - Fork 11
WPF. Error templates
You can use default theme for error templates, but there are three reasons why it's better to use templates from library:
- You don't like default WPF error theme (because it doesn't display messages).
- You want to use
Warning
level with orange brush instead of red. - You need to change localization in run-time .
There are two error templates in library:
- Default WPF theme with supporting different brushes (if it's OK to you with 1 and 3 reasons):
- Error template with popups with messages:
There are two way how you can extract this templates.
At your App.xaml merge resource dictionary from library:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/ReactiveValidation.Wpf;component/Themes/Generic.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
And use it in your control:
<TextBox Validation.ErrorTemplate="{StaticResource WpfErrorTemplate}" />
<!-- or -->
<TextBox Validation.ErrorTemplate="{StaticResource ExtendedErrorTemplate}" />
It's also a good idea to create style with error template:
<Style TargetType="TextBox">
<Setter Property="Validation.ErrorTemplate" Value="{StaticResource WpfErrorTemplate}" />
</Style>
<!-- or -->
<Style TargetType="TextBox">
<Setter Property="Validation.ErrorTemplate" Value="{StaticResource ExtendedErrorTemplate}" />
</Style>
xmlns:t="clr-namespace:ReactiveValidation.WPF.Templates;assembly=ReactiveValidation.Wpf"
...
<TextBox Validation.ErrorTemplate="{x:Static t:ErrorTemplates.WpfErrorTemplate}" />
<!-- or -->
<TextBox Validation.ErrorTemplate="{x:Static t:ErrorTemplates.ExtendedErrorTemplate}" />
It's also a good idea to create style with error template:
xmlns:t="clr-namespace:ReactiveValidation.WPF.Templates;assembly=ReactiveValidation.Wpf"
...
<Style TargetType="TextBox">
<Setter Property="Validation.ErrorTemplate" Value="{x:Static t:ErrorTemplates.WpfErrorTemplate}" />
</Style>
<!-- or -->
<Style TargetType="TextBox">
<Setter Property="Validation.ErrorTemplate" Value="{x:Static t:ErrorTemplates.ExtendedErrorTemplate}" />
</Style>
Templates using ValidationErrorBrush
for errors and ValidationWarningBrush
for warnings:
<SolidColorBrush x:Key="ValidationErrorBrush" Color="Red" />
<SolidColorBrush x:Key="ValidationWarningBrush" Color="Orange" />
ExtendedErrorTemplate
also using two brushes for popus:
<SolidColorBrush x:Key="ValidationMessageForegroundBrush" Color="Black" />
<SolidColorBrush x:Key="ValidationMessageBackgroundBrush" Color="White" />
You can easily override this brushes at your application:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/ReactiveValidation.Wpf;component/Themes/Generic.xaml" />
</ResourceDictionary.MergedDictionaries>
<SolidColorBrush x:Key="ValidationErrorBrush" Color="Purple" />
<SolidColorBrush x:Key="ValidationWarningBrush" Color="Green" />
<SolidColorBrush x:Key="ValidationMessageForegroundBrush" Color="White" />
<SolidColorBrush x:Key="ValidationMessageBackgroundBrush" Color="Blue" />
</ResourceDictionary>
</Application.Resources>
and result:
System.Windows.Controls.ErrorsProperty
contains list of object
. ReactiveValidate
puts there objects of type ReactiveValidation.ValidationMessage
.
- For support run-time localization - create
DataTemplate
forReactiveValidation.ValidationMessage
type. Put insideTextBlock
or whatever you want and bind it toValidationMessage.Message
property. It's supportINotifyPropertyChanged
, so if localization changed - message will changed too. - Supporting
Warning
's a bit harder. You should check all elements atErrorsProperty
if they are allWarning
orSimpleWarning
.
You can use templates in library as sample. They are located here.