ItemsControl.AlternationCount 속성

정의

번갈아 가며 컨테이너가 고유한 모양을 가질 수 있도록 하는 번갈아 있는 항목 ItemsControl컨테이너 수를 가져오거나 설정합니다.

public:
 property int AlternationCount { int get(); void set(int value); };
[System.ComponentModel.Bindable(true)]
public int AlternationCount { get; set; }
[<System.ComponentModel.Bindable(true)>]
member this.AlternationCount : int with get, set
Public Property AlternationCount As Integer

속성 값

에 있는 ItemsControl번갈아 가며 항목 컨테이너의 수입니다.

특성

예제

다음 예제 ListBox 에서는 상속되는 ItemsControl항목 컨테이너(형식 ListBoxItem)가 번갈아 가며 각 컨테이너에 대해 다른 배경 및 포그라운드를 지정합니다. 이 예제에서는 BackgroundForeground 속성을 ItemsControl.AlternationIndex 바인딩하고 각 속성에 AlternationConverter 대해 제공합니다.

<Grid>
  <Grid.Resources>
    <AlternationConverter x:Key="BackgroundConverter">
      <SolidColorBrush>Blue</SolidColorBrush>
      <SolidColorBrush>CornflowerBlue</SolidColorBrush>
      <SolidColorBrush>LightBlue</SolidColorBrush>
    </AlternationConverter>

    <AlternationConverter x:Key="AlternateForegroundConverter">
      <SolidColorBrush>White</SolidColorBrush>
      <SolidColorBrush>Black</SolidColorBrush>
      <SolidColorBrush>Navy</SolidColorBrush>
    </AlternationConverter>

    <Style x:Key="alternatingWithBinding" TargetType="{x:Type ListBoxItem}">
      <Setter Property="Background" 
              Value="{Binding RelativeSource={RelativeSource Self},
                     Path=(ItemsControl.AlternationIndex),
                     Converter={StaticResource BackgroundConverter}}"/>

      <Setter Property="Foreground" 
              Value="{Binding RelativeSource={RelativeSource Self},
                     Path=(ItemsControl.AlternationIndex),
                     Converter={StaticResource AlternateForegroundConverter}}"/>
    </Style>

  </Grid.Resources>

  <ListBox AlternationCount="3" ItemsSource="{StaticResource data}"
           ItemContainerStyle="{StaticResource alternatingWithBinding}"/>
</Grid>

다음 예제에서는 개체를 사용하여 Trigger 이전 예제와 동일하게 수행합니다.

<Grid>
  <Grid.Resources>
    <Style x:Key="alternatingWithTriggers" TargetType="{x:Type ListBoxItem}">
      <Setter Property="Background" Value="Blue"/>
      <Setter Property="Foreground" Value="White"/>
      <Style.Triggers>
        <Trigger Property="ListBox.AlternationIndex" Value="1">
          <Setter Property="Background" Value="CornflowerBlue"/>
          <Setter Property="Foreground" Value="Black"/>
        </Trigger>
        <Trigger Property="ListBox.AlternationIndex" Value="2">
          <Setter Property="Background" Value="LightBlue"/>
          <Setter Property="Foreground" Value="Navy"/>
        </Trigger>
      </Style.Triggers>
    </Style>

  </Grid.Resources>
  <ListBox AlternationCount="3" ItemsSource="{StaticResource data}" 
           ItemContainerStyle="{StaticResource alternatingWithTriggers}">
  </ListBox>
</Grid>

설명

AlternationCountItemsControl.AlternationIndex 속성을 사용하면 두 개 이상의 번갈아 있는 항목 컨테이너에 대한 모양을 지정할 수 있습니다. 예를 들어 , 에 있는 모든 세 번째 항목에 대해 번갈아 배경색을 지정할 수 있습니다 ItemsControl. 의 ItemsControl.AlternationIndex 각 항목 컨테이너에 ItemsControl할당됩니다. ItemsControl.AlternationIndex 는 0에서 시작하여 1을 뺀 값이 AlternationCount 될 때까지 증분한 다음 0에서 다시 시작합니다. 예를 들어 3이고 7개의 항목이 있는 AlternationCount경우 ItemsControl 다음 표에는 각 항목에 ItemsControl.AlternationIndex 대한 목록이 나와 있습니다.

에 있는 항목의 위치 ItemsControl ItemsControl.AlternationIndex
1 0
2 1
3 2
4 0
5 1
6 2
7 0

번갈아 있는 항목 컨테이너에 대해 다양한 모양을 지정하는 데 사용할 수 있는 몇 가지 방법이 있습니다. 한 가지 방법은 항목 컨테이너의 속성을 .에 바인딩하는 것입니다 ItemsControl.AlternationIndex. 그런 다음, 특정 AlternationConverter 값이 ItemsControl.AlternationIndex 있는 항목 컨테이너에 적용할 값을 지정하는 데 사용할 수 있습니다. 트리거를 사용하여 해당 값에 따라 항목 컨테이너의 속성 값을 ItemsControl.AlternationIndex변경할 수도 있습니다.

적용 대상