ItemsControl.AlternationIndex Propriedade Anexada

Definição

Obtém o valor atribuído ao contentor de itens quando são usados recipientes alternados.

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

Exemplos

O exemplo seguinte especifica que o ListBox (que herda de ItemsControl) tem recipientes de itens alternados (que são do tipo ListBoxItem) e especifica um fundo e primeiro plano diferentes para cada um. O exemplo associa as Background propriedades e Foreground ao ItemsControl.AlternationIndex e fornece um AlternationConverter para cada propriedade.

<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>

O exemplo seguinte faz o mesmo que o anterior, usando Trigger objetos.

<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>

Observações

As AlternationCount propriedades e ItemsControl.AlternationIndex permitem-lhe especificar a aparência de dois ou mais recipientes de itens alternados. Por exemplo, pode especificar cores de fundo alternadas para cada terceiro item de um ItemsControl. O ItemsControl.AlternationIndex é atribuído a cada contentor de itens no ItemsControl. ItemsControl.AlternationIndex começa em 0, incrementa até ser AlternationCount menos 1, e depois recomeça em 0. Por exemplo, se AlternationCount for 3 e houver sete itens no ItemsControl, a tabela seguinte lista o ItemsControl.AlternationIndex para cada item.

Posição do item no ItemsControl ItemsControl.AlternationIndex
1 0
2 1
3 2
4 0
5 1
6 2
7 0

Existem vários métodos que pode usar para especificar diferentes aparências para os recipientes alternados dos objetos. Um método é associar propriedades do recipiente dos itens ao ItemsControl.AlternationIndex. Podes então usar um AlternationConverter para especificar qual valor deve ser aplicado ao contentor de itens que tem um determinado ItemsControl.AlternationIndex valor. Também pode usar gatilhos para alterar o valor da propriedade de um contentor de itens dependendo do valor do seu ItemsControl.AlternationIndex.

Aplica-se a