Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I provide multiple conditions for data trigger in WPF?

How can I provide multiple conditions for data trigger in WPF?

like image 551
Sumeru Suresh Avatar asked May 25 '09 08:05

Sumeru Suresh


People also ask

What is multi trigger in WPF?

In the previous chapter, we worked with triggers to get dynamic styles. So far they have all been based on a single property, but WPF also supports multi triggers, which can monitor two or more property conditions and only trigger once all of them are satisfied.

How many types of triggers are there in WPF?

Basically, there are 3 types of triggers, they are: Property Trigger. Data Trigger. Event Trigger.

What is DataTrigger in WPF?

A DataTrigger allows you to set property values when the property value of the data object matches a specified Value. For example, if you are displaying a list of Employee objects, you may want the foreground color to be different based on each Employee's current attendance.


2 Answers

Use MultiDataTrigger type

<Style TargetType="ListBoxItem">     <Style.Triggers>       <DataTrigger Binding="{Binding Path=State}" Value="WA">         <Setter Property="Foreground" Value="Red" />       </DataTrigger>           <MultiDataTrigger>         <MultiDataTrigger.Conditions>           <Condition Binding="{Binding Path=Name}" Value="Portland" />           <Condition Binding="{Binding Path=State}" Value="OR" />         </MultiDataTrigger.Conditions>         <Setter Property="Background" Value="Cyan" />       </MultiDataTrigger>     </Style.Triggers>   </Style> 
like image 74
Gishu Avatar answered Sep 24 '22 17:09

Gishu


@jasonk - if you want to have "or" then negate all conditions since (A and B) <=> ~(~A or ~B)

but if you have values other than boolean try using type converters:

<MultiDataTrigger.Conditions>     <Condition Value="True">         <Condition.Binding>             <MultiBinding Converter="{StaticResource conditionConverter}">                 <Binding Path="Name" />                 <Binding Path="State" />             </MultiBinding>         </Condition.Binding>         <Setter Property="Background" Value="Cyan" />     </Condition> </MultiDataTrigger.Conditions> 

you can use the values in Convert method any way you like to produce a condition which suits you.

like image 31
serine Avatar answered Sep 24 '22 17:09

serine