Skip to main content
.NET Framework Class Library
AlternationConverter Class

Converts an integer to and from an object by applying the integer as an index to a list of objects.

Inheritance Hierarchy
SystemObject
  System.Windows.ControlsAlternationConverter

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
Syntax
<[%$TOPIC/cc452249_en-us_VS_110_2_0_0_0_0%]("Values")> _
Public Class AlternationConverter _
	Implements [%$TOPIC/cc452249_en-us_VS_110_2_0_0_0_1%]
[[%$TOPIC/cc452249_en-us_VS_110_2_0_1_0_0%]("Values")]
public class AlternationConverter : [%$TOPIC/cc452249_en-us_VS_110_2_0_1_0_1%]
[[%$TOPIC/cc452249_en-us_VS_110_2_0_2_0_0%](L"Values")]
public ref class AlternationConverter : [%$TOPIC/cc452249_en-us_VS_110_2_0_2_0_1%]
[<[%$TOPIC/cc452249_en-us_VS_110_2_0_3_0_0%]("Values")>]
type AlternationConverter =  
    class 
        interface [%$TOPIC/cc452249_en-us_VS_110_2_0_3_0_1%] 
    end
<AlternationConverter>
  [%$TOPIC/cc452249_en-us_VS_110_2_0_4_0_0%]
</AlternationConverter>

The AlternationConverter type exposes the following members.

Constructors
  NameDescription
Public method AlternationConverterInitializes a new instance of the AlternationConverter class.
Top
Properties
  NameDescription
Public property ValuesGets a list of objects that the AlternationConverter returns when an integer is passed to the Convert method.
Top
Methods
  NameDescription
Public method ConvertConverts an integer to an object in the Values list.
Public method ConvertBackConverts an object in the Values list to an integer.
Public method Equals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected method FinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method GetHashCodeServes as the default hash function. (Inherited from Object.)
Public method GetTypeGets the Type of the current instance. (Inherited from Object.)
Protected method MemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public method ToStringReturns a string that represents the current object. (Inherited from Object.)
Top
Remarks

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:

Examples

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

.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Platforms

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.

Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.