Uređivanje

Dijeli putem


HierarchicalDataTemplate.AlternationCount Property

Definition

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

public:
 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 for the next level of items.

Examples

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>

Remarks

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.

Applies to