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

プロパティ値

Int32

ItemsControl 内の代替項目コンテナーの数。

属性

次の例では、(継承元ItemsControl) に交互の項目コンテナー (種類ListBoxItem) があることを指定ListBoxし、それぞれ異なる背景と前景を指定します。 この例では、プロパティと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 使用すると、2 つ以上の交互の項目コンテナーの外観を指定できます。 たとえば、3 つ目の項目ごとに交互の背景色を ItemsControl指定できます。 内の ItemsControl.AlternationIndex 各項目コンテナーに ItemsControl割り当てられます。 ItemsControl.AlternationIndex は 0 から始まり、マイナス 1 になるまで AlternationCount インクリメントし、0 で再起動します。 たとえば、3 で 7 つの項目がある場合AlternationCountは、次の表に各項目の一覧をItemsControl.AlternationIndexItemsControlします。

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

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

適用対象