GroupStyle.AlternationCount Property
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Gets or sets the number of alternating GroupItem objects.
public:
property int AlternationCount { int get(); void set(int value); };
public int AlternationCount { get; set; }
member this.AlternationCount : int with get, set
Public Property AlternationCount As Integer
Property Value
The number of alternating GroupItem objects.
Examples
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:
Sets AlternationCount to 2.
Creates an AlternationConverter that returns a SolidColorBrush, depending on the value of ItemsControl.AlternationIndex.
Binds the Background of the TextBlock in the HeaderTemplate to the ItemsControl.AlternationIndex property and provides the AlternationConverter. Note that the AlternationIndex attached property is set on the GroupItem, and the binding uses FindAncestor to get its value.
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>
Remarks
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.
Applies to
.NET