AlternationConverter Class
Updated: July 2008
Converts an integer to and from an object by applying the integer as an index to a list of objects.
Assembly: PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation
You can use the AlternationConverter to bind a property to alternating values. For example, if you want items in a ListBox to have alternating blue and light blue backgrounds, you do the following:
Create an AlternationConverter and add a blue SolidColorBrush and a light blue SolidColorBrush to its Values property.
In a Style for ListBoxItem, bind the Background property to ItemsControl.AlternationIndex and assign the converter you created to the Binding.Converter property.
The following example creates a ListBox and uses two AlternationConverter objects to alternate the Background and FontStyle of the items.
<Grid> <Grid.Resources> <AlternationConverter x:Key="BackgroundConverter"> <SolidColorBrush>Blue</SolidColorBrush> <SolidColorBrush>LightBlue</SolidColorBrush> </AlternationConverter> <AlternationConverter x:Key="FontStyleConverter"> <FontStyle >Italic</FontStyle> <FontStyle >Normal</FontStyle> </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="FontStyle" Value="{Binding RelativeSource={RelativeSource Self}, Path=(ItemsControl.AlternationIndex), Converter={StaticResource FontStyleConverter}}"/> </Style> </Grid.Resources> <ListBox AlternationCount="2" ItemsSource="{StaticResource data}" ItemContainerStyle="{StaticResource alternatingWithBinding}"/> </Grid>
Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.