語言

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>

備註

AlternationCount和 ItemsControl.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

你可以用多種方法來指定交替物品容器的不同外觀。 一種方法是將 item container 的屬性綁定到 ItemsControl.AlternationIndex。 接著你可以用 來 AlternationConverter 指定應該套用哪個值給具有特定 ItemsControl.AlternationIndex 值的物品容器。 你也可以用觸發器來根據物品 ItemsControl.AlternationIndex容器的值來改變物品容器屬性的值。

適用於