Trigger when condition is not equal to

2020-04-02 18:12发布

I need a Style under WPF that sets several properties when multiple conditions are fullfilled. However, one of my conditions is of type Not Equal To. How should I change the below Style so that the condition would become Not Equal To? Can it be even achieved without IValueConverter?

<Style>
    <Style.Triggers>
        <MultiDataTrigger>
             <MultiDataTrigger.Conditions>
                  <!--<Condition 1 here.../>-->
                  <!--<Condition 2 here.../>-->
                  <Condition Binding="{Binding Path=id}" Value="3"/>
             </MultiDataTrigger.Conditions>
             <Setter Property="Background" Value="Red"/>
             <Setter Property="Foreground" Value="Black"/>
         </MultiDataTrigger>
    </Style.Triggers>
</Style>

I would need the below but this of course don't work since Triggers only support Equal operator.

<Style>
    <Style.Triggers>
        <MultiDataTrigger>
             <MultiDataTrigger.Conditions>
                  <!--<Condition 1 here.../>-->
                  <!--<Condition 2 here.../>-->
                  <Condition Binding="{Binding Path=id}" Value<>"3"/>
             </MultiDataTrigger.Conditions>
             <Setter Property="Background" Value="Red"/>
             <Setter Property="Foreground" Value="Black"/>
         </MultiDataTrigger>
    </Style.Triggers>
</Style>

标签: wpf styles
2条回答
地球回转人心会变
2楼-- · 2020-04-02 18:27

You need an IValueConverter and some extra markup for this:

    <Style>
        <Style.Triggers>
            <MultiDataTrigger>
                <MultiDataTrigger.Conditions>
                    <!--<Condition 1 here.../>-->
                    <!--<Condition 2 here.../>-->
                    <Condition>
                        <Condition.Binding>
                            <Binding Path="id" Converter="{StaticResource ValueToEqualsParameterConverter}">
                                <Binding.ConverterParameter>
                                    <System:Int32>3</System:Int32>
                                </Binding.ConverterParameter>
                            </Binding>
                        </Condition.Binding>
                        <Condition.Value>
                            <System:Boolean>False</System:Boolean>
                        </Condition.Value>
                    </Condition>
                </MultiDataTrigger.Conditions>
                <Setter Property="Background" Value="Red" />
                <Setter Property="Foreground" Value="Black" />
            </MultiDataTrigger>
        </Style.Triggers>
    </Style>

And the converter:

public class ValueToEqualsParameterConverter : IValueConverter {
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { 
        return value == parameter; 
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { 
        return null; 
    }
}
查看更多
别忘想泡老子
3楼-- · 2020-04-02 18:38

Another option is to define default value as setter in style and then implement data trigger. In following code, the background value is always red except when value is 3

<Style>  
  <Setter Property="Background" Value="Red"/>
  <Setter Property="Foreground" Value="Black"/>
    <Style.Triggers>
        <MultiDataTrigger>
             <MultiDataTrigger.Conditions>
                  <!--<Condition 1 here.../>-->
                  <!--<Condition 2 here.../>-->
                  <Condition Binding="{Binding Path=id}" Value="3"/>
             </MultiDataTrigger.Conditions>
             <Setter Property="Background" Value="DefaultColor"/>
             <Setter Property="Foreground" Value="DefaultColor2"/>
         </MultiDataTrigger>
    </Style.Triggers>
</Style>

查看更多
登录 后发表回答