语言

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,并且有 7 个项目, 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。

适用于