Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trigger when condition is not equal to

Tags:

styles

wpf

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>
like image 570
Nuts Avatar asked Nov 21 '14 08:11

Nuts


People also ask

Can you have multiple triggers in Power Automate?

As we all probably know, every Flow in Power Automate begins with a trigger and continues with multiple actions, branches and conditions. But have you ever considered putting a trigger in the middle of a flow? That's right. It is entirely possible to put another trigger inside of an action.

How do you trigger a flow in Power Automate?

In Power Automate, select My flows. Select + New flow, and from the drop-down select Instant cloud flow. Alternatively, you can select Automated cloud flow to create a flow that is triggered by a specific event, like the creation of a new file in an OneDrive for Business account.


2 Answers

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; 
    }
}
like image 165
Ross Avatar answered Oct 27 '22 08:10

Ross


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>
like image 30
Disha Avatar answered Oct 27 '22 09:10

Disha