ItemsControl.AlternationIndex 添付プロパティ

定義

交互の項目コンテナーが使用されている場合に、項目コンテナーの割り当てられた値を取得します。

see GetAlternationIndex, and SetAlternationIndex
see GetAlternationIndex
see GetAlternationIndex, and SetAlternationIndex
see GetAlternationIndex
see GetAlternationIndex, and SetAlternationIndex
see GetAlternationIndex

次の例では、(ListBox から継承される) ItemsControlに交互の項目コンテナー (ListBoxItem型) があることを指定し、それぞれに異なる背景と前景を指定します。 この例では、 Background プロパティと Foreground プロパティを 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>

注釈

AlternationCountプロパティと ItemsControl.AlternationIndex プロパティを使用すると、2 つ以上の交互の項目コンテナーの外観を指定できます。 たとえば、 ItemsControlの 3 つ目の項目ごとに背景色を交互に指定できます。 ItemsControl.AlternationIndexは、ItemsControl内の各項目コンテナーに割り当てられます。 ItemsControl.AlternationIndex は 0 から始まり、 AlternationCount マイナス 1 になるまでインクリメントされ、0 で再起動されます。 たとえば、 AlternationCount が 3 で、 ItemsControlに 7 つの項目がある場合、次の表に各項目の ItemsControl.AlternationIndex を示します。

内の項目の位置 ItemsControl ItemsControl.AlternationIndex
1 0
2 1
3 2
4 0
5 1
6 2
7 0

交互の項目コンテナーに異なる外観を指定するために使用できる方法はいくつかあります。 1 つの方法は、項目コンテナーのプロパティを ItemsControl.AlternationIndexにバインドすることです。 その後、 AlternationConverter を使用して、特定の ItemsControl.AlternationIndex 値を持つ項目コンテナーに適用する値を指定できます。 トリガーを使用して、 ItemsControl.AlternationIndexの値に応じて項目コンテナーのプロパティの値を変更することもできます。

適用対象