共用方式為


HOW TO:繫結至列舉

更新:2007 年 11 月

本範例示範如何透過繫結至列舉型別的 GetValues 方法,繫結到列舉型別。

範例

在下列範例中,ListBox 會顯示整個資料繫結中 HorizontalAlignment 列舉值的清單。ListBoxButton 已經繫結在一起,您只要從 ListBox 中選取一個值,就能變更 ButtonHorizontalAlignment 屬性值。

<Window
  xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:sys="clr-namespace:System;assembly=mscorlib" 
  SizeToContent="WidthAndHeight" 
  Title="Show Enums in a ListBox using Binding">

  <Window.Resources>
    <ObjectDataProvider MethodName="GetValues"
                        ObjectType="{x:Type sys:Enum}"
                        x:Key="AlignmentValues">
      <ObjectDataProvider.MethodParameters>
        <x:Type TypeName="HorizontalAlignment" />
      </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>
  </Window.Resources>

  <Border Margin="10" BorderBrush="Aqua"
          BorderThickness="3" Padding="8">
    <StackPanel Width="300">
      <TextBlock>Choose the HorizontalAlignment value of the Button:</TextBlock>
      <ListBox Name="myComboBox" SelectedIndex="0" Margin="8"
               ItemsSource="{Binding Source={StaticResource AlignmentValues}}"/>
      <Button Content="Click Me!"
              HorizontalAlignment="{Binding ElementName=myComboBox,
                                            Path=SelectedItem}"/>
    </StackPanel>
  </Border>
</Window>

請參閱

工作

HOW TO:繫結至方法

概念

資料繫結概觀

其他資源

資料繫結範例

資料繫結 HOW TO 主題