Windows Presentation Foundation
How to: Bind to an Enumeration
This example shows how to bind to an enumeration by binding to the enumeration's GetValues method.
Example
In the following example, the ListBox displays the list of HorizontalAlignment enumeration values through data binding. The ListBox and the Button are bound such that you can change the HorizontalAlignment property value of the Button by selecting a value in the ListBox.
XAML
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://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>
See Also
Tasks
Concepts
Other Resources
Community Content
dsandor
Note: When using your own enum type.
When using your own enum instead of a built in enum as this example shows, you will need to also make a xmlns reference to the namespace that contains your enum. Additionally you will need to include the namespace prefix in the x:Type markup extension. Here is an example doing this:
<Window x:Class="Warehouse.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:warehouse="clr-namespace:Warehouse.Model;assembly=Warehouse.Model"
Title="Dock Receipts" Height="478" Width="957" xmlns:my="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView">
<Window.Resources>
<ObjectDataProvider MethodName="GetValues"
ObjectType="{x:Type sys:Enum}"
x:Key="ReceiptTypeEnum">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="warehouse:DockReceiptTypeEnum" />
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</Window.Resources>
x:Type markup extension is documented here:
Further details here:
http://davidsandor.com/blogs/tipsandtricks/archive/2009/07/09/wpf-how-to-bind-to-an-enum-in-xaml.aspx