Alternate Row Color In ListBox In WPF

Introduction

 
In this article we will see how we can make the Alternate Rows of the ListBox Colorful in WPF.
 

Creating WPF Project

 
Fire up Visual Studio 2008 and create a new WPF Project. Name it as ListBoxSampleWPF.
 
Alternate Row Color In ListBox In WPF
 
Now we will add a ListBox to the Application and it will look as following,
 
Alternate Row Color In ListBox In WPF
 
Now let's add some sample data to the ListBox.
  1. public class Person  
  2.     {  
  3.         public string Name { getset; }  
  4.         public int Age { getset; }  
  5.         public string Country { getset; }  
  6.     }  
  7. ObservableCollection<Person> myList;  
  8.         public Window1()  
  9.         {  
  10.             InitializeComponent();  
  11.             myList = new ObservableCollection<Person>()  
  12.             {  
  13.                 new Person{ Name="Name 1", Age=24, Country="India"},  
  14.                 new Person{ Name="Name 2", Age=24, Country="India"},  
  15.                 new Person{ Name="Name 3", Age=24, Country="India"},  
  16.                 new Person{ Name="Name 4", Age=24, Country="India"},  
  17.                 new Person{ Name="Name 5", Age=24, Country="India"},  
  18.                 new Person{ Name="Name 6", Age=24, Country="India"},  
  19.                 new Person{ Name="Name 7", Age=24, Country="India"},  
  20.                 new Person{ Name="Name 8", Age=24, Country="India"},  
  21.                 new Person{ Name="Name 9", Age=24, Country="India"},  
  22.                 new Person{ Name="Name 10", Age=24, Country="India"},  
  23.                 new Person{ Name="Name 11", Age=24, Country="India"},  
  24.                 new Person{ Name="Name 12", Age=24, Country="India"},  
  25.                 new Person{ Name="Name 13", Age=24, Country="India"},  
  26.                 new Person{ Name="Name 14", Age=24, Country="India"},  
  27.                 new Person{ Name="Name 15", Age=24, Country="India"},  
  28.                 new Person{ Name="Name 16", Age=24, Country="India"},  
  29.                 new Person{ Name="Name 17", Age=24, Country="India"},  
  30.                 new Person{ Name="Name 18", Age=24, Country="India"},  
  31.                 new Person{ Name="Name 19", Age=24, Country="India"},  
  32.                 new Person{ Name="Name 20", Age=24, Country="India"},  
  33.                 new Person{ Name="Name 21", Age=24, Country="India"},  
  34.                 new Person{ Name="Name 22", Age=24, Country="India"},  
  35.             };  
  36.             lbPersonList.ItemsSource = myList;  
  37.         }  
Now Bind the Property "Name" to the ListBox, so that we can see the Names.
  1. <ListBox x:Name="lbPersonList" Margin="19,17,162,25">  
  2.             <ListBox.ItemTemplate>  
  3.                 <DataTemplate>  
  4.                     <TextBlock Text="{Binding Name}"/>  
  5.                 </DataTemplate>  
  6.             </ListBox.ItemTemplate>  
  7. </ListBox>  
Alternate Row Color In ListBox In WPF
 
If you see from the above wecannot differentiate with each row, I mean to say the Row Background color is all same white.
 
Now we would add a style to the ListBoxItem.
  1. <Window.Resources>  
  2.         <Style  TargetType="{x:Type ListBoxItem}">  
  3.             <Style.Triggers>  
  4.                 <Trigger Property="ItemsControl.AlternationIndex" Value="0">  
  5.                     <Setter Property="Background" Value="#19f39611"></Setter>  
  6.                 </Trigger>  
  7.                 <Trigger Property="ItemsControl.AlternationIndex" Value="1">  
  8.                     <Setter Property="Background" Value="#19000000"></Setter>  
  9.                 </Trigger>  
  10.             </Style.Triggers>  
  11.         </Style>  
  12. </Window.Resources>  
  13.   
  14. <ListBox x:Name="lbPersonList" Margin="19,17,162,25" AlternationCount="2">  
  15.             <ListBox.ItemTemplate>  
  16.                 <DataTemplate>  
  17.                     <TextBlock Text="{Binding Name}"/>  
  18.                 </DataTemplate>  
  19.             </ListBox.ItemTemplate>  
  20. </ListBox>  
Now run the application.
 
Alternate Row Color In ListBox In WPF
 
Yes, we got alternate row colors.
 
We can have value more than 2 as AlternationCount and add that many colors and you will see the Repeatation of colors after the AlternationCount.
 
Hope this article helps.