Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validation Error Templates For UserControl

Tags:

wpf

I have built an UserControl. I don't like the red border showing around it when validation errors occur. I have a textbox inside my control.

How can I override the validation error style to get rid of the red border in the whole control and just show a red background in the textbox inside my usercontrol?

Thanks!

like image 290
Luis Aguilar Avatar asked Oct 18 '11 14:10

Luis Aguilar


1 Answers

I am using this template that will color the background of the textbox instead of showing just the border.

 <UserControl.Resources>
    <Style TargetType="{x:Type TextBox}">
        <Style.Triggers>
            <Trigger Property="Validation.HasError" Value="true" >
                <Setter Property="Foreground" Value="Red"/>
                <Setter Property="Background" Value="MistyRose"/>
                <Setter Property="BorderBrush" Value="Red"/>
                <Setter Property="BorderThickness" Value="1.0"/>
                <Setter Property="VerticalContentAlignment" Value="Center"/>
                <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource 
                 Self},Path=(Validation.Errors)[0].ErrorContent}"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</UserControl.Resources>

And all I have to do to your DocPannel Where the controls are located for example for me inside a DockPanel then i have to set its Validation.Error template to nothing this will remove the border.

For Ex:

    <TextBox >
       <Validation.ErrorTemplate>
         <ControlTemplate>
         </ControlTemplate>
       </Validation.ErrorTemplate>
     </TextBox>
like image 106
Nivid Dholakia Avatar answered Oct 13 '22 00:10

Nivid Dholakia