This documentation is archived and is not being maintained.

HierarchicalDataTemplate.AlternationCount Property

Updated: July 2008

Gets or sets the number of alternating item containers for the child items.

Namespace:  System.Windows
Assembly:  PresentationFramework (in PresentationFramework.dll)

Public Property AlternationCount As Integer
Dim instance As HierarchicalDataTemplate 
Dim value As Integer 

value = instance.AlternationCount

instance.AlternationCount = value
<object AlternationCount="int" .../>

Property Value

Type: System.Int32
The number of alternating item containers for the next level of items.

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
















When you set the AlternationCount property, you are indicating that the child items should get an ItemsControl.AlternationIndex in that range, not the item that has this HierarchicalDataTemplate applied to it. For example, if a HeaderedItemsControl called aHeaderedItemsControl uses a HierarchicalDataTemplate with the AlternationCount set, the item containers of the child items of aHeaderedItemsControl will have an ItemsControl.AlternationIndex, not the item container for aHeaderedItemsControl.

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.

The following example creates a TreeView that is bound to data that is three levels deep, and each item is displayed in a TextBlock. The TextBlock objects in the first level have the same property values, the TextBlock objects in the second level use alternating values for the FontStyle property, and the TextBlock objects in the third level use alternating values for the Background property.

Because the HierarchicalDataTemplate for the first level, Level1Data, has the AlternationCount property set to 2, the ItemsControl.AlternationIndex for the TreeViewItem objects in the second level alternates between 0 and 1. In the HierarchicalDataTemplate for the second level, Level2Data, the FontStyle of the TextBlock is bound to the ItemsControl.AlternationIndex and an AlternationConverter is provided to convert the ItemsControl.AlternationIndex to an alternating FontStyle. A similar relationship exists between Level2Data and Level3Data: The AlternationCount is set to 3 on Level2Data, and the TextBlock in Level3Ddata has its Background property bound to the ItemsControl.AlternationIndex.


    <!--Returns alternating brushes.-->
    <AlternationConverter x:Key="TeamsBackgroundConverter">

    <!--The DataTemplate used by TreeViewItems in the third level
    of the TreeView.-->
    <DataTemplate x:Key="Level3Data">
      <TextBlock Text="{Binding Path=Name}"
        Background="{Binding RelativeSource={RelativeSource FindAncestor, 
           AncestorType={x:Type TreeViewItem}},
           Converter={StaticResource TeamsBackgroundConverter}}"/>

    <!--Returns altnernating FontStyles.-->
    <AlternationConverter x:Key="LeagueFontStyleConverter">
      <FontStyle >Italic</FontStyle>
      <FontStyle >Normal</FontStyle>

    <!--The HierarchicalDataTemplate used by TreeViewItems
     in the second level of the TreeView.-->
    <HierarchicalDataTemplate x:Key="Level2Data"
      ItemsSource="{Binding Path=Teams}"
      ItemTemplate="{StaticResource Level3Data}"
      <TextBlock Text="{Binding Path=Name}"
        FontStyle="{Binding RelativeSource={RelativeSource FindAncestor, 
           AncestorType={x:Type TreeViewItem}},
           Converter={StaticResource LeagueFontStyleConverter}}"/>

    <!--The HierarchicalDataTemplate used by TreeViewItems
     in the first level of the TreeView.-->
    <HierarchicalDataTemplate x:Key="Level1Data"
      ItemsSource="{Binding Path=Divisions}"
      ItemTemplate="{StaticResource Level2Data}"
      <TextBlock Text="{Binding Path=Name}" FontWeight="Bold"/>

    <Style TargetType="TreeViewItem">
      <Setter Property="IsExpanded" Value="True"/>

  <TreeView ItemsSource="{Binding Source={StaticResource MyTreeViewData}}"
            ItemTemplate="{StaticResource Level1Data}"/>

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5 SP1, 3.0 SP2




July 2008

Added topic for new member.

SP1 feature change.