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 세 가지 수준으로 데이터에 바인딩된 및 각 항목에 표시 됩니다는 TextBlock합니다. TextBlock 첫 번째 수준에 있는 개체에는 동일한 속성 값을 합니다 TextBlock 교대로 반복 되는 값을 사용 하는 두 번째 수준에 있는 개체를 FontStyle 속성인 및 TextBlock 교대로 반복 되는 값을 사용 하는 세 번째 수준에 있는 개체를 Background 속성입니다.

때문에 HierarchicalDataTemplate 첫 번째 수준에 대 한 Level1DataAlternationCount 속성이 2로 설정 합니다 ItemsControl.AlternationIndex 에 대 한는 TreeViewItem 0과 1 사이의 두 번째 수준 대체에서 개체. 에 HierarchicalDataTemplate 두 번째 수준에 대 한 Level2Data, FontStyleTextBlock 바인딩되는 ItemsControl.AlternationIndexAlternationConverter 변환 하기 위해 제공 됩니다는 ItemsControl.AlternationIndex 를 교대로 반복 되 FontStyle. 유사한 테이블 간에 관계가 Level2DataLevel3Data:를 AlternationCount 에서 3으로 설정 됩니다 Level2Data, 및 TextBlock 에서 Level3Ddata 에 해당 Background 속성에 바인딩된는 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>

설명

합니다 AlternationCountItemsControl.AlternationIndex 속성을 사용 하면 두 개 이상의 교대로 반복 되는 항목 컨테이너에 대 한 모양을 지정할 수 있습니다. 예를 들어, 모든 세 번째 항목에 대해 교대로 반복 되는 배경 색을 지정할 수 있습니다는 ItemsControl합니다. ItemsControl.AlternationIndex 각 항목 컨테이너에 할당 되는 ItemsControl합니다. ItemsControl.AlternationIndex 0에서 시작 될 때까지 증가, AlternationCount 1이 고, 한 다음 다시 0에서 뺀 값입니다. 예를 들어, 경우 AlternationCount 이 3이 고에 7 개의 항목이 있는 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합니다.

적용 대상