HierarchicalDataTemplate.AlternationCount 屬性

定義

取得或設定子項目的替代項目容器個數。

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

屬性值

Int32

下一層級項目的替代項目容器個數。

範例

下列範例會建立系 TreeView 結至三層深度之資料的 ,且每個專案都會顯示在 中 TextBlockTextBlock第一層中的物件具有相同的屬性值、 TextBlock 第二層中的物件會使用屬性的替代值 FontStyle ,而 TextBlock 第三層中的物件則使用屬性的 Background 替代值。

HierarchicalDataTemplate因為第一層的 , Level1Data 所以 AlternationCount 屬性會設定為 2, ItemsControl.AlternationIndex TreeViewItem 第二層中的 物件會在 0 到 1 之間替代。 HierarchicalDataTemplate在 第二層的 中, Level2Data TextBlock FontStyle 的 會系結至 , ItemsControl.AlternationIndex 並提供 AlternationConverter 將 轉換為 ItemsControl.AlternationIndex 替代 FontStyle 的 。 和 之間 Level2Data 存在類似的關聯性:在 上 Level2Data 設定 AlternationCount 為 3,而 TextBlock 中的 Level3Ddata 屬性 Background 已系結至 ItemsControl.AlternationIndexLevel3Data

<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>

備註

AlternationCountItemsControl.AlternationIndex 屬性可讓您指定兩個或多個替代專案容器的外觀。 例如,您可以指定 中 ItemsControl 每三個專案的替代背景色彩。 會 ItemsControl.AlternationIndex 指派給 中的每個 ItemsControl 專案容器。 ItemsControl.AlternationIndex 從 0 開始,遞增直到 AlternationCount 減 1,然後在 0 重新開機。 例如,如果 AlternationCount 是 3,而且 中有 ItemsControl 七個專案,下表會列出 ItemsControl.AlternationIndex 每個專案的 。

專案在 中的位置 ItemsControl ItemsControl.AlternationIndex
1 0
2 1
3 2
4 0
5 1
6 2
7 0

當您設定 AlternationCount 屬性時,表示子專案應該在該範圍內取得 ItemsControl.AlternationIndex ,而不是套用此 HierarchicalDataTemplate 專案的專案。 例如,如果呼叫 HeaderedItemsControl aHeaderedItemsControl 使用 HierarchicalDataTemplate 具有 集合的 AlternationCount ,則 的子專案 aHeaderedItemsControl 容器會有 ItemsControl.AlternationIndex ,而不是 的專案 aHeaderedItemsControl 容器。

有數種方法可用來指定替代專案容器的不同外觀。 其中一種方法是將專案容器的屬性系結至 ItemsControl.AlternationIndex 。 然後 AlternationConverter ,您可以使用 來指定應該套用至具有特定 ItemsControl.AlternationIndex 值的專案容器的值。 您也可以使用觸發程式,根據專案容器的值來變更專案 ItemsControl.AlternationIndex 容器屬性的值。

適用於