Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF - How to combine DataTrigger and Trigger?

NOTE I have asked the related question: How to combine DataTrigger and EventTrigger?

I have a list box containing several items. The item's class implements INotifyPropertyChanged and has a property IsAvailable. I use that property to indicate unavailable options in the list using a different colour.

However, if a selected item is not available, then the foreground colour should be red.

<ListBox>   <ListBox.Resources>     <DataTemplate DataType="{x:Type local:InstitutionViewModel}">       <TextBlock Name="Name" Text="{Binding Name}"/>       <DataTemplate.Triggers>         <DataTrigger Binding="{Binding IsAvailable}" Value="False">           <Setter TargetName="Name" Property="Foreground" Value="#888"/>         </DataTrigger>       </DataTemplate.Triggers>     </DataTemplate>   </ListBox.Resources> </ListBox> 

I use the above data trigger to grey out unavailable items.

The problem I'm facing is that the fact that the item is selected has nothing to do with the underlying data to which the template is bound. What I really want is some kind of multi-trigger that supports both a regular Trigger on a dependency property (ListBoxItem.IsSelected) along with a DataTrigger on the bound data item.

Can this be done without introducing the concept of selection into my view model?

For anyone wondering why I do not disable unavailable items, understand that it is a requirement of the application that unavailable options may be selected. There are actually a few list boxes, and selection in one effects what's available in the others. I cannot disable the items as the user would not be able to change their minds or explore different combinations if items were disabled based upon earlier selections.

like image 785
Drew Noakes Avatar asked Mar 02 '09 14:03

Drew Noakes


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.

What is trigger and how many types of triggers in WPF?

Triggers are used to create visual effects on controls and framework elements. Triggers are parts of styles and are always defined inside a style. Basically, there are 3 types of triggers, they are: Property 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

For anyone else who's up against this problem, I found a solution that works for me. Of course, I'm still interested to see other interesting answers.

Here's what I did:

<MultiDataTrigger>   <MultiDataTrigger.Conditions>     <Condition Binding="{Binding       RelativeSource={         RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}},         Path=IsSelected}" Value="True"/>     <Condition Binding="{Binding IsAvailable}" Value="False"/>   </MultiDataTrigger.Conditions>   <Setter TargetName="Name" Property="Foreground" Value="#F00"/> </MultiDataTrigger> 

There's nothing special about this being a multi trigger though. If you just wanted to style the selected item differently in your data template, you could use:

<DataTrigger Binding="{Binding    RelativeSource={     RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}},     Path=IsSelected}" Value="True">   <Setter TargetName="Name" Property="Foreground" Value="#888"/> </DataTrigger> 
like image 134
Drew Noakes Avatar answered Sep 17 '22 12:09

Drew Noakes


To use it with DataGridRow change binding mode to Self:

Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=...  
like image 38
Vadim Avatar answered Sep 17 '22 12:09

Vadim