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 中的交替項目容器數目。

屬性

範例

下列範例指定 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 開始,遞增直到 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

有數種方法可用來指定替代專案容器的不同外觀。 其中一種方法是將專案容器的屬性系結至 ItemsControl.AlternationIndex 。 然後 AlternationConverter ,您可以使用 來指定應該套用至具有特定 ItemsControl.AlternationIndex 值的專案容器的值。 您也可以使用觸發程式,根據專案容器的值來變更專案 ItemsControl.AlternationIndex 容器屬性的值。

適用於