AlternationConverter 類別

定義

將整數當做索引套用至物件清單,藉此在整數與物件之間來回轉換。

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
繼承
AlternationConverter
屬性
實作

範例

下列範例會建立 ,並使用兩個 ListBox 物件來替代 Background 專案的 和 FontStyleAlternationConverter

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

備註

您可以使用 將 AlternationConverter 屬性系結至替代值。 例如,如果您想要 中的 ListBox 專案具有交替的藍色和淺藍色背景,請執行下列動作:

建構函式

AlternationConverter()

初始化 AlternationConverter 類別的新執行個體。

屬性

Values

當整數傳遞至 AlternationConverter 方法時,取得 Convert(Object, Type, Object, CultureInfo) 傳回的物件清單。

方法

Convert(Object, Type, Object, CultureInfo)

將整數轉換成 Values 清單中的物件。

ConvertBack(Object, Type, Object, CultureInfo)

Values 清單中的物件轉換成整數。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

適用於