ItemsControl.AlternationCount Property


Gets or sets the number of alternating item containers in the ItemsControl, which enables alternating containers to have a unique appearance.

 property int AlternationCount { int get(); void set(int value); };
public int AlternationCount { get; set; }
member this.AlternationCount : int with get, set
Public Property AlternationCount As Integer

Property Value

The number of alternating item containers in the ItemsControl.



The following example specifies that the ListBox (which inherits from ItemsControl) has alternating item containers (which are of type ListBoxItem) and specifies a different background and foreground for each one. The example binds the Background and Foreground properties to the ItemsControl.AlternationIndex and provides an AlternationConverter for each property.

    <AlternationConverter x:Key="BackgroundConverter">

    <AlternationConverter x:Key="AlternateForegroundConverter">

    <Style x:Key="alternatingWithBinding" TargetType="{x:Type ListBoxItem}">
      <Setter Property="Background" 
              Value="{Binding RelativeSource={RelativeSource Self},
                     Converter={StaticResource BackgroundConverter}}"/>

      <Setter Property="Foreground" 
              Value="{Binding RelativeSource={RelativeSource Self},
                     Converter={StaticResource AlternateForegroundConverter}}"/>


  <ListBox AlternationCount="3" ItemsSource="{StaticResource data}"
           ItemContainerStyle="{StaticResource alternatingWithBinding}"/>

The following example does the same as the previous example by using Trigger objects.

    <Style x:Key="alternatingWithTriggers" TargetType="{x:Type ListBoxItem}">
      <Setter Property="Background" Value="Blue"/>
      <Setter Property="Foreground" Value="White"/>
        <Trigger Property="ListBox.AlternationIndex" Value="1">
          <Setter Property="Background" Value="CornflowerBlue"/>
          <Setter Property="Foreground" Value="Black"/>
        <Trigger Property="ListBox.AlternationIndex" Value="2">
          <Setter Property="Background" Value="LightBlue"/>
          <Setter Property="Foreground" Value="Navy"/>

  <ListBox AlternationCount="3" ItemsSource="{StaticResource data}" 
           ItemContainerStyle="{StaticResource alternatingWithTriggers}">


The AlternationCount and ItemsControl.AlternationIndex properties enable you to specify the appearance for two or more alternating item containers. For example, you can specify alternating background colors for every third item in an ItemsControl. The ItemsControl.AlternationIndex is assigned to each item container in the ItemsControl. ItemsControl.AlternationIndex begins at 0, increments until it is AlternationCount minus 1, and then restarts at 0. For example, if AlternationCount is 3 and there are seven items in the ItemsControl, the following table lists the ItemsControl.AlternationIndex for each item.

Position of Item in the ItemsControl ItemsControl.AlternationIndex
1 0
2 1
3 2
4 0
5 1
6 2
7 0

There are several methods you can use to specify different appearances for the alternating item containers. One method is to bind properties of the items container to the ItemsControl.AlternationIndex. You can then use an AlternationConverter to specify which value should be applied to the item container that has a certain ItemsControl.AlternationIndex value. You can also use triggers to change the value of an item container's property depending on the value of its ItemsControl.AlternationIndex.

Applies to