Posted by: Zeeshan Amjad | August 24, 2009

Multiple conditions in Data Triggers


We can specify more than one condition in data trigger. Lets take a look at the example. Suppose we have a list of states name and cities and we want to apply different colors depending on if the city is the capital or the state or the largest city of the state. Here is the class of city.

  1:     public class CityInfo
  2:     {
  3:         public CityInfo()
  4:         {
  5:         }
  6: 
  7:         public CityInfo(String state, String city,
  8:             bool isCapital, bool isLargest)
  9:         {
 10:             State = state;
 11:             City = city;
 12:             IsCapital = isCapital;
 13:             IsLargest = isLargest;
 14:         }
 15: 
 16:         public String State
 17:         { set; get; }
 18: 
 19:         public String City
 20:         { set; get; }
 21: 
 22:         public bool IsCapital
 23:         { set; get; }
 24: 
 25:         public bool IsLargest
 26:         { set; get; }
 27:     }
 28: 

Here is the simple XAML code to apply multiple data trigger.

  1: <Window x:Class="TypeConvertor.Window1"
  2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4:     Title="Data Trigger Demo" Height="300" Width="400" Loaded="Window_Loaded">
  5:     
  6:     <Window.Resources>
  7:         <Style TargetType="{x:Type ListViewItem}">
  8:             <Style.Triggers>
  9:                 <DataTrigger Binding="{Binding IsCapital}" Value="true">
 10:                     <Setter Property="Background" Value="Red"/>
 11:                     <Setter Property="Foreground" Value="White"/>
 12:                 </DataTrigger>
 13:                 <DataTrigger Binding="{Binding IsLargest}" Value="true">
 14:                     <Setter Property="Background" Value="Blue"/>
 15:                     <Setter Property="Foreground" Value="White"/>
 16:                 </DataTrigger>
 17:             </Style.Triggers>
 18:         </Style>
 19:     </Window.Resources>
 20:     
 21:     <Grid>                
 22:         <ListView Name="lstView">
 23:             <ListView.View>
 24:                 <GridView>
 25:                     <GridViewColumn Width="180" Header="State" DisplayMemberBinding="{Binding Path = State}"/>
 26:                     <GridViewColumn Width="180" Header="City" DisplayMemberBinding="{Binding Path = City}"/>
 27:                 </GridView>
 28:             </ListView.View>
 29:         </ListView>
 30:     </Grid>
 31: </Window>
 32: 

Now we are going to insert multiple cities into list control.

  1:         private void Window_Loaded(object sender, RoutedEventArgs e)
  2:         {
  3:             lstView.Items.Add(new CityInfo("California", "Los Angeles", false, true));
  4:             lstView.Items.Add(new CityInfo("California", "San Francisco", false, false));
  5:             lstView.Items.Add(new CityInfo("California", "sacramento", true, false));
  6:             lstView.Items.Add(new CityInfo("California", "San Diego", false, false));            
  7:             lstView.Items.Add(new CityInfo("Massachusetts", "Boston", true, true));
  8:         }
  9: 

Here is the output of this program

Logical_Trigger_01

Here note the last row. Boston is not only the capital of Massachusetts but also the largest city. Therefore both  condition applies here. If more than one condition apply on data then the last condition will apply .

Advertisements

Responses

  1. […] already saw few example of data trigger here and here. But sometimes we want to execute the negative condition. Such as trigger should execute when the […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: