Category / Section
How to set the ValidationError template for WPF MaskedEdit control?
1 min read
You can do the validation in WPF MaskedEdit (SfMaskedEdit). If you want to show the error in different style or format, you need to use the Validation.ErrorTemplate. This template used to assign the custom template for the validation done in WPF MaskedEdit (SfMaskedEdit). Please find the code for the same from following:
XAML
<Grid.Resources> <!--Validation template used for Error template--> <ControlTemplate x:Key="validationErrorTemplate"> <DockPanel> <TextBlock Foreground="Red" DockPanel.Dock="Top">Error</TextBlock> <AdornedElementPlaceholder x:Name="ErrorAdorner"/> </DockPanel> </ControlTemplate> </Grid.Resources> <StackPanel> <TextBlock Margin="5" Text="MaskedEdit"/> <sf:SfMaskedEdit x:Name="maskededit" Margin="5" Validation.ErrorTemplate="{StaticResource validationErrorTemplate}" ValidationMode="{Binding ElementName=validation ,Path=Text}" Mask="00/00/0000" MaskType="Simple" > <sf:SfMaskedEdit.Style> <Style TargetType="sf:SfMaskedEdit"> <Style.Triggers> <Trigger Property="ValidationMode" Value="KeyPress"> <Setter Property="Text"> <Setter.Value> <Binding Path="Value" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged"> <Binding.ValidationRules> <local:NameValidator/> </Binding.ValidationRules> </Binding> </Setter.Value> </Setter> </Trigger> <Trigger Property="ValidationMode" Value="LostFocus"> <Setter Property="Text"> <Setter.Value> <Binding Path="Value" Mode="TwoWay" UpdateSourceTrigger="LostFocus"> <Binding.ValidationRules> <local:NameValidator/> </Binding.ValidationRules> </Binding> </Setter.Value> </Setter> </Trigger> </Style.Triggers> </Style> </sf:SfMaskedEdit.Style> </sf:SfMaskedEdit> <TextBlock Margin="5" Text="TextBox"/> <TextBox Margin="5"/> <TextBlock Margin="5" Text="ValidationMode"/> <ComboBox x:Name="validation" Margin="5" SelectedIndex="0"> <ComboBoxItem Content="LostFocus"/> <ComboBoxItem Content="KeyPress"/> </ComboBox> </StackPanel>
Sample: ValidationError template for SfMaskedEdit