WPF Combobox Validation.ErrorTemplate Error

I have a combo box that needs to be edited for its error pattern to show a red frame when there is a validation error.

I use the following style

<Style TargetType="{x:Type ComboBox}" > <Setter Property="Validation.ErrorTemplate"> <Setter.Value> <ControlTemplate> <DockPanel> <Border BorderBrush="Red" BorderThickness="3"> <AdornedElementPlaceholder /> </Border> </DockPanel> </ControlTemplate> </Setter.Value> </Setter> <Setter Property="FontFamily" Value="Segoe UI" /> <Setter Property="FontSize" Value="12" /> <Setter Property="VerticalAlignment" Value="Center" /> </Style> 

A border never appears on validation errors. Any tips on what's going wrong?

+4
source share
5 answers

Style You published the work. You need to check your binding, have you added ValidatesOnDataErrors=True and ValidatesOnExceptions=True to the SelectedValue binding?

+4
source

enter heretry code without the dock panel, which is unresponsive since it completes jus one element. However, without much caution, I don’t know if it makes sense to wrap the text box with a border, since it already has a border! You should try to directly change the color of your border. You can try using the panel again, but then place the border around the panel, i.e.:

Border BorderBrush = "Red" BorderThickness = "3"
Dockpanel
Adornedelement

This is more important because the wrapper panel does not have its own border.

+1
source

Use this.

  <Style x:Key="textBoxStyle" TargetType="{x:Type telerik:RadMaskedTextBox}"> <Style.Triggers> <Trigger Property="Validation.HasError" Value="True"> <Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors)[0].ErrorContent}"/> <Setter Property="Control.BorderBrush" Value="Red" /> </Trigger> </Style.Triggers> </Style> 
+1
source

I do not like any of the answers here. Simply put, how do I change the border color for an error template for a ComboBox using Blend or not? Do not accept another border around the existing ComboBox border. I figured out how to create a ControlTemplate in Blend, but not a Validation template.

I came close, trying to make it look like I had changed the actual border color, but that’s not what I really want. Suggestions? To complicate it, I would like to display a red asterisk outside the right border of the control.

The following code is a close attempt, but it actually draws a frame inside the ComboBox, and if you look closely, you will see that the border is 2 pixels wide in combination with the ComboBox border:

 <DockPanel Name="myDockPanel"> <AdornedElementPlaceholder> <Border BorderBrush="Blue" BorderThickness="1" CornerRadius="2" /> </AdornedElementPlaceholder> <TextBlock Text="*" FontWeight="Bold" FontSize="14" Foreground="Red" DockPanel.Dock="Left" ToolTip="{Binding .CurrentItem}" /> </DockPanel> 
+1
source

I searched for a few more articles and came up with a solution based on another article here: WPF - How to apply a style to AdornedElement for AdornedElementPlaceholder?

 <!-- This works --> <ComboBox Name="comboBox1" Style="{StaticResource NewComboBoxStyle}" Validation.ErrorTemplate="{StaticResource comboBoxValidationTemplate}" /> <SolidColorBrush x:Key="MainBorderBrush">#FF91B3FF</SolidColorBrush> <Style x:Key="NewComboBoxStyle" TargetType="{x:Type ComboBox}" BasedOn="{StaticResource myErrorTemplate}"> <Setter Property="BorderBrush" Value="{DynamicResource MainBorderBrush}" /> <Style.Triggers> <Trigger Property="Validation.HasError" Value="True"> <Setter Property="BorderBrush" Value="Blue" /> </Trigger> </Style.Triggers> </Style> <!-- Sets ToolTip when Validation.HasError is True. --> <Style TargetType="Control" x:Key="myErrorTemplate"> <Style.Triggers> <Trigger Property="Validation.HasError" Value="true"> <Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors).CurrentItem.ErrorContent}"/> </Trigger> </Style.Triggers> </Style> <ControlTemplate x:Key="comboBoxValidationTemplate"> <DockPanel Name="myDockPanel"> <AdornedElementPlaceholder/> <TextBlock Text="*" FontWeight="Bold" FontSize="14" Foreground="Red" DockPanel.Dock="Left" ToolTip="{Binding .CurrentItem}" /> </DockPanel> </ControlTemplate> 
0
source

All Articles