Szerkesztés

Megosztás a következőn keresztül:


AlternationConverter Class

Definition

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

public ref class AlternationConverter : System::Windows::Data::IValueConverter
[System.Windows.Markup.ContentProperty("Values")]
public class AlternationConverter : System.Windows.Data.IValueConverter
[<System.Windows.Markup.ContentProperty("Values")>]
type AlternationConverter = class
    interface IValueConverter
Public Class AlternationConverter
Implements IValueConverter
Inheritance
AlternationConverter
Attributes
Implements

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>

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:

Constructors

AlternationConverter()

Initializes a new instance of the AlternationConverter class.

Properties

Values

Gets a list of objects that the AlternationConverter returns when an integer is passed to the Convert(Object, Type, Object, CultureInfo) method.

Methods

Convert(Object, Type, Object, CultureInfo)

Converts an integer to an object in the Values list.

ConvertBack(Object, Type, Object, CultureInfo)

Converts an object in the Values list to an integer.

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ToString()

Returns a string that represents the current object.

(Inherited from Object)

Applies to