Home > front end >  Using DataTrigger on Validation.ErrorTemplate property
Using DataTrigger on Validation.ErrorTemplate property

Time:01-26

I'm trying to set up different styling for a border around a textbox for both errors and warnings. Currently I am able to get the default error styling color to show but implementing warnings with DataTrigger doesn't seem to update the styling on the border. The DataTrigger is supposed to be bound to a bool value in an object in (Validation.Errors)[0].ErrorContext outside of Windows.Resources.

 <Style x:Name="ErrTemplate" x:Key="ErrTemplate">
      <Setter Property="Validation.ErrorTemplate">
          <Setter.Value>
               <ControlTemplate>
                    <AdornedElementPlaceholder x:Uid="ControlWithErrors" Name="ControlWithErrors>
                         <Border x:Name="MainBorder" BorderBrush="Red" BorderThickness="1"/>
                    </AdornedElementPlaceholder>
                    <ControlTemplate.Triggers>
                         <DataTrigger Binding="{Binding (Validation.Errors)[0].ErrorContent.IsWarning, ElementName="ControlWithErrors}" Value="true">
                              <Setter TargetName="MainBorder" Property="BorderBrush" Value="Green"/>
                         </DataTrigger>
                    </ControlTemplate.Triggers>
               </ControlTemplate>
          </Setter.Value>
     <Setter>
</Style>    

And the element outside of Windows.Resources that has notification on validation errors whenever an invalid name is word is typed

<TextBox
     x:Uid="EmailName"
     x:Name="EmailName"
     Text="{Binding EmailName, Mode="TwoWay", NotifyOnValidationError=True, ValidatesOnNotifyDataErrors=True, UpdateSourcetrigger=PropertyChanged}"
     Style="{DynamicResource ErrTemplate}"/>

Is there a better way of doing this? Any help would be great.

CodePudding user response:

I think the error is that you set BorderBrush="Red" as local value. Set it in a style and it should then work, if the binding is correct.
See Dependency property value precedence (WPF .NET)

<Style x:Name="ErrTemplate" x:Key="ErrTemplate">
    <Setter Property="Validation.ErrorTemplate">
        <Setter.Value>
            <ControlTemplate>
                <AdornedElementPlaceholder x:Uid="ControlWithErrors" Name="ControlWithErrors">
                    <Border x:Name="MainBorder" BorderThickness="1">
                        <Border.Style>
                            <Style TargetType="Border">
                                <Setter Property="BorderBrush" Value="Red"/>
                            </Style>
                        </Border.Style>
                    </Border>
                </AdornedElementPlaceholder>
                <ControlTemplate.Triggers>
                    <DataTrigger Binding="{Binding [0].ErrorContent.IsWarning}" Value="true">
                        <Setter TargetName="MainBorder" Property="BorderBrush" Value="Green"/>
                    </DataTrigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
  • Related