HierarchicalDataTemplate.AlternationCount Property

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

Namespace:  System.Windows
Assembly:  PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation

'Declaration
Public Property AlternationCount As Integer
<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

ItemsControl.AlternationIndex

1

0

2

1

3

2

4

0

5

1

6

2

7

0

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.

<StackPanel>
  <StackPanel.Resources>

    <!--Returns alternating brushes.-->
    <AlternationConverter x:Key="TeamsBackgroundConverter">
      <SolidColorBrush>LimeGreen</SolidColorBrush>
      <SolidColorBrush>SpringGreen</SolidColorBrush>
      <SolidColorBrush>Chartreuse</SolidColorBrush>
    </AlternationConverter>

    <!--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}},
           Path=(ItemsControl.AlternationIndex),
           Converter={StaticResource TeamsBackgroundConverter}}"/>
    </DataTemplate>

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

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

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

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

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

.NET Framework

Supported in: 4.5, 4, 3.5 SP1, 3.0 SP2

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft