Trigger when condition is not equal

I need a Style in WPF that sets multiple properties when filling in multiple conditions. However, one of my conditions is of type Not Equal To . How do I change the Style below so that the condition becomes Not Equal To ? Can this be done even without an 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 will need the following, but this, of course, does not work, since triggers only support the 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> 
+5
source share
2 answers

To do this, you need an IValueConverter and an additional add-in:

  <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; } } 
+9
source

Another option is to define the default value as an installer in the style, and then implement a data trigger. In the following code, the background value is always red, except that the 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
source

Source: https://habr.com/ru/post/1207379/


All Articles