Search code examples
wpfdatagriddatatrigger

How to run a DataTrigger based on if Validation.HasError = False?


On my DataGridTextColumn I would like to run a DataTrigger but only when Validation.HasError is False

This is what I have at the moment:

<DataGridTextColumn Header="Volts" 
                Binding="{Binding DcVolts, Converter={StaticResource StringToDecimalConverter}}" 
                Width="Auto">
<DataGridTextColumn.ElementStyle>
    <Style TargetType="{x:Type TextBlock}" 
           BasedOn="{StaticResource DataGridTextColumnElementErrorStyle}">
        <Style.Triggers>
            <MultiDataTrigger>
                <MultiDataTrigger.Conditions>
                    <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=Validation.HasError}" 
                               Value="False" />
                    <Condition Value="False">
                        <Condition.Binding>
                            <MultiBinding Converter="{StaticResource EqualityConverter}">
                                <Binding Path="DcVolts" />
                                <Binding Path="DcSpecVolts" />
                            </MultiBinding>
                        </Condition.Binding>
                    </Condition>
                </MultiDataTrigger.Conditions>
                <MultiDataTrigger.Setters>
                    <Setter Property="Background" Value="Orange" />
                </MultiDataTrigger.Setters>
            </MultiDataTrigger>
        </Style.Triggers>
    </Style>
</DataGridTextColumn.ElementStyle>

The thinking behind is that Validation.HasError has to be False for the MultiDataTrigger to set the BackGround to Orange if the result of the EqualityConverter is False

This is because if Validation.HasError is True then I want the usual Pink background that my DataGridTextColumnElementErrorStyle provides when a rule I have set on the Property fails.

I actually got closer with this:

<DataGridTextColumn Header="Volts" 
                Binding="{Binding DcVolts, Converter={StaticResource StringToDecimalConverter}}" 
                Width="Auto">
<DataGridTextColumn.ElementStyle>
    <Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource DataGridTextColumnElementErrorStyle}">
        <Style.Triggers>
            <DataTrigger Value="False">
                <DataTrigger.Binding>
                    <MultiBinding Converter="{StaticResource EqualityConverter}" >
                        <Binding Path="DcVolts" />
                        <Binding Path="DcSpecVolts" />
                    </MultiBinding>
                </DataTrigger.Binding>
                <Setter Property="Background" Value="Orange" />
            </DataTrigger>
            <Trigger Property="Validation.HasError" Value="True">
                <Setter Property="Background" Value="Pink" />
            </Trigger>
        </Style.Triggers>
    </Style>
</DataGridTextColumn.ElementStyle>

But the problem was that on Validation.HasError the messages provided (by the rules I have created) in the ToolTip where duplicated.

Any advice is greatly appreciated.


Solution

  • You should add parentheses around the binding path since Validation.HasError is an attached property:

    <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.HasError)}" 
               Value="True" />