Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Tagged as

Alternative Item Backgrounds The .NET 3.5 SP1 Way

, 9 Apr 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
The .NET 3.5 SP1 is full of tricks, just today I found a new property called AlternationCount which is available on all the ItemControl controls such as ItemControl, ListBox etc etc.This simply property can be used to provide some sort of alternative look based on the current value of the Alternati

The .NET 3.5 SP1 is full of tricks, just today I found a new property called AlternationCount which is available on all the ItemControl controls such as ItemControl, ListBox etc etc.

This simply property can be used to provide some sort of alternative look based on the current value of the AlternationIndex. So you can color items or format them differently based on their index.

Here is a small example where I color the items, a different color based on their AlternationIndex value. The AlternationIndex works hand in hand with the AlternationCount of the ItemsControl (or derived control), so in this example the ItemsControl AlternationCount is 3, which means we can have triggers on the AlternationIndex for values 1/2 or 3.

Here is the XAML for a small example

   1:  <Window x:Class="WpfApplication1.Window1"
   2:      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   3:      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   4:      Title="Window1" Height="300" Width="300">
   5:      
   6:      <Window.Resources>
   7:  
   8:          <DataTemplate x:Key="someStringItemsTemplate">
   9:  
  10:              <Border x:Name="bord" BorderBrush="Transparent" 
  11:                      CornerRadius="0"
  12:                      HorizontalAlignment="Stretch"
  13:                      Background="#ff656565">
  14:                  <Label Content="{Binding}"
  15:                         HorizontalContentAlignment="Stretch"
  16:                         HorizontalAlignment="Stretch"
  17:                         Margin="0"
  18:                         Background="Transparent"/>
  19:  
  20:              </Border>
  21:              <DataTemplate.Triggers>
  22:                  <Trigger Property="ItemsControl.AlternationIndex" 
  23:                           Value="1">
  24:                      <Setter TargetName="bord" 
  25:                           Property="Background" Value="Yellow"/>
  26:                  </Trigger>
  27:                  <Trigger Property="ItemsControl.AlternationIndex" 
  28:                           Value="2">
  29:                      <Setter TargetName="bord" 
  30:                           Property="Background" Value="Orange"/>
  31:                  </Trigger>
  32:              </DataTemplate.Triggers>
  33:          </DataTemplate>
  34:  
  35:      </Window.Resources>
  36:  
  37:      <ItemsControl x:Name="someItems" AlternationCount="3"
  38:                        VerticalAlignment="Top"
  39:                        HorizontalAlignment="Stretch"
  40:                        Margin="0,0,0,0"
  41:                        ItemTemplate="{StaticResource 
  42:                          someStringItemsTemplate}">
  43:          <ItemsControl.ItemsPanel>
  44:              <ItemsPanelTemplate>
  45:                  <StackPanel Orientation="Vertical"/>
  46:              </ItemsPanelTemplate>
  47:          </ItemsControl.ItemsPanel>
  48:  
  49:      </ItemsControl>
  50:  </Window>

And when run with a small set of items we get

image-thumb8.png

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Sacha Barber
Software Developer (Senior)
United Kingdom United Kingdom
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)
 
- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence
 
Both of these at Sussex University UK.
 
Award(s)

I am lucky enough to have won a few awards for Zany Crazy code articles over the years

  • Microsoft C# MVP 2014
  • Codeproject MVP 2014
  • Microsoft C# MVP 2013
  • Codeproject MVP 2013
  • Microsoft C# MVP 2012
  • Codeproject MVP 2012
  • Microsoft C# MVP 2011
  • Codeproject MVP 2011
  • Microsoft C# MVP 2010
  • Codeproject MVP 2010
  • Microsoft C# MVP 2009
  • Codeproject MVP 2009
  • Microsoft C# MVP 2008
  • Codeproject MVP 2008
  • And numerous codeproject awards which you can see over at my blog

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.1411023.1 | Last Updated 9 Apr 2009
Article Copyright 2009 by Sacha Barber
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid