Dieser Artikel wurde maschinell übersetzt. Wenn Sie die englische Version des Artikels anzeigen möchten, aktivieren Sie das Kontrollkästchen Englisch. Sie können den englischen Text auch in einem Popupfenster anzeigen, indem Sie den Mauszeiger über den Text bewegen.
Übersetzung
Englisch

ItemsControl.AlternationCount-Eigenschaft

 

Veröffentlicht: Oktober 2016

Ruft die Anzahl der Container für abwechselnde Elemente im ItemsControl-Objekt ab, oder legt diese fest. Dadurch können abwechselnde Container unterschiedlich dargestellt werden.

Namespace:   System.Windows.Controls
Assembly:  PresentationFramework (in PresentationFramework.dll)

[BindableAttribute(true)]
public int AlternationCount { get; set; }

Eigenschaftswert

Type: System.Int32

Die Anzahl der abwechselnden Elementcontainer im ItemsControl-Objekt.

The P:System.Windows.Controls.ItemsControl.AlternationCount and P:System.Windows.Controls.ItemsControl.AlternationIndex properties enable you to specify the appearance for two or more alternating item containers. For example, you can specify alternating background colors for every third item in an T:System.Windows.Controls.ItemsControl. The P:System.Windows.Controls.ItemsControl.AlternationIndex is assigned to each item container in the T:System.Windows.Controls.ItemsControl. P:System.Windows.Controls.ItemsControl.AlternationIndex begins at 0, increments until it is P:System.Windows.Controls.ItemsControl.AlternationCount minus 1, and then restarts at 0. For example, if P:System.Windows.Controls.ItemsControl.AlternationCount is 3 and there are seven items in the T:System.Windows.Controls.ItemsControl, the following table lists the P:System.Windows.Controls.ItemsControl.AlternationIndex for each item.

Position of Item in the T:System.Windows.Controls.ItemsControl

ItemsControl.AlternationIndex

1

0

2

1

3

2

4

0

5

1

6

2

7

0

There are several methods you can use to specify different appearances for the alternating item containers. One method is to bind properties of the items container to the P:System.Windows.Controls.ItemsControl.AlternationIndex. You can then use an T:System.Windows.Controls.AlternationConverter to specify which value should be applied to the item container that has a certain P:System.Windows.Controls.ItemsControl.AlternationIndex value. You can also use triggers to change the value of an item container's property depending on the value of its P:System.Windows.Controls.ItemsControl.AlternationIndex.

The following example specifies that the T:System.Windows.Controls.ListBox (which inherits from T:System.Windows.Controls.ItemsControl) has alternating item containers (which are of type T:System.Windows.Controls.ListBoxItem) and specifies a different background and foreground for each one. The example binds the P:System.Windows.Controls.Control.Background and P:System.Windows.Controls.Control.Foreground properties to the P:System.Windows.Controls.ItemsControl.AlternationIndex and provides an T:System.Windows.Controls.AlternationConverter for each property.

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

The following example does the same as the previous example by using T:System.Windows.Trigger objects.

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

.NET Framework
Verfügbar seit 3.0
Zurück zum Anfang
Anzeigen: