Export (0) Print
Expand All

GroupStyle.AlternationCount Property

Gets or sets the number of alternating GroupItem objects.

Namespace:  System.Windows.Controls
Assembly:  PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation

public int AlternationCount { get; set; }
<object AlternationCount="int" .../>

Property Value

Type: System.Int32
The number of alternating GroupItem objects.

The AlternationCount property and the ItemsControl.AlternationIndex attached property enable you to specify the appearance for two or more alternating GroupItem objects. For example, you can specify alternating background colors for every third GroupItem in an ItemsControl. The ItemsControl.AlternationIndex is assigned to each GroupItem in the ItemsControl. ItemsControl.AlternationIndex begins at 0, increments until it is AlternationCount minus 1, and then restarts at 0. For example, if AlternationCount is 3 and there are seven GroupItem objects in the ItemsControl, the following table lists the ItemsControl.AlternationIndex for each item.

Position of GroupItem in the 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 GroupItem objects. One method is to bind properties in the HeaderTemplate or ContainerStyle of the GroupStyle to the ItemsControl.AlternationIndex. You can then use an AlternationConverter to specify which value should be applied to the GroupItem that has a certain ItemsControl.AlternationIndex value. You can also use triggers to change the value of a property depending on the value of its ItemsControl.AlternationIndex.

The following example creates a ListBox that is bound to a CollectionViewSource that groups the items in the collection. The example alternates the background for the header of each GroupItem between two colors by doing the following:

The example also alternates the background of the items in the ListBox between three colors by setting the AlternationCount on the ListBox and binding the Background of each ListBox to the ItemsControl.AlternationIndex. In this case, the background alternates between three colors.

The CollectionViewSource that groups the items is not shown. For information on how to group item, see How to: Sort and Group Data Using a View in XAML.

<StackPanel>

  <StackPanel.Resources>

    <!--Returns a Brush for the header of a GroupItem.-->
    <AlternationConverter x:Key="GroupHeaderBackgroundConverter">
      <SolidColorBrush>LightBlue</SolidColorBrush>
      <SolidColorBrush>LightSteelBlue</SolidColorBrush>
    </AlternationConverter>

    <!--Returns a Brush for a ListBoxItem.-->
    <AlternationConverter x:Key="BackgroundConverter">
      <SolidColorBrush>Silver</SolidColorBrush>
      <SolidColorBrush>LightGray</SolidColorBrush>
      <SolidColorBrush>GhostWhite</SolidColorBrush>
    </AlternationConverter>

  </StackPanel.Resources>

  <ListBox ItemsSource="{Binding Source={StaticResource groupedData}}"
           DisplayMemberPath="CityName" AlternationCount="3" Name="lb">

    <ListBox.GroupStyle>
      <!--Set alternating backgrounds on the header of each group.-->
      <GroupStyle AlternationCount="2">
        <GroupStyle.HeaderTemplate>
          <DataTemplate>
            <TextBlock FontWeight="Bold" 
                       Text="{Binding Path=Name}" 
                       Background="{Binding 
                           RelativeSource={RelativeSource FindAncestor, 
                           AncestorType={x:Type GroupItem}},
                           Path=(ItemsControl.AlternationIndex),
                           Converter={StaticResource 
                                      GroupHeaderBackgroundConverter}}"/>
          </DataTemplate>
        </GroupStyle.HeaderTemplate>
      </GroupStyle>
    </ListBox.GroupStyle>

    <ListBox.ItemContainerStyle>
      <!--Set alternating backgrounds on the items in the ListBox.-->
      <Style TargetType="{x:Type ListBoxItem}">
        <Setter Property="Background" 
                Value="{Binding RelativeSource={RelativeSource Self},
                     Path=(ItemsControl.AlternationIndex),
                     Converter={StaticResource BackgroundConverter}}"/>
      </Style>
    </ListBox.ItemContainerStyle>
  </ListBox>
</StackPanel>

.NET Framework

Supported in: 4.5, 4, 3.5 SP1, 3.0 SP2

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft