Trigger when condition is not equal to

前端 未结 2 1693
再見小時候
再見小時候 2021-01-11 17:39

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

相关标签:
2条回答
  • 2021-01-11 18:04

    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>

    0 讨论(0)
  • 2021-01-11 18:06

    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; 
        }
    }
    
    0 讨论(0)
提交回复
热议问题