How to bind to an enumeration (WPF .NET)
This example shows how to bind to an enumeration. Unfortunately there isn't a direct way to use an enumeration as a data binding source. However, the Enum.GetValues(Type) method returns a collection of values. These values can be wrapped in an ObjectDataProvider and used as a data source.
The ObjectDataProvider type provides a convenient way to create an object in XAML and use it as a data source.
Important
The Desktop Guide documentation for .NET 7 and .NET 6 is under construction.
Reference the enumeration
Use the ObjectDataProvider type to wrap an array of enumeration values provided by the enumeration type itself.
Create a new
ObjectDataProvider
as a XAML resource, either in your application XAML or the XAML of the object you're working with. This example uses a window and creates theObjectDataProvider
with a resource key ofEnumDataSource
.<Window.Resources> <ObjectDataProvider x:Key="EnumDataSource" ObjectType="{x:Type sys:Enum}" MethodName="GetValues"> <ObjectDataProvider.MethodParameters> <x:Type TypeName="HorizontalAlignment" /> </ObjectDataProvider.MethodParameters> </ObjectDataProvider> </Window.Resources>
In this example, the
ObjectDataProvider
uses three properties to retrieve the enumeration:Property Description ObjectType
The type of object to be returned by the data provider. In this example, System.Enum. The sys:
XAML namespace is mapped toSystem
.MethodName
The name of the method to run on the System.Enum
type. In this example, Enum.GetValues.MethodParameters
A collection of values to provide to the MethodName
method. In this example, the method takes theSystem.Type
of the enumeration.Effectively, the XAML is breaking down a method call, method name, parameters, and the return type. The
ObjectDataProvider
configured in the previous example is the equivalent of the following code:var enumDataSource = System.Enum.GetValues(typeof(System.Windows.HorizontalAlignment));
Dim enumDataSource = System.Enum.GetValues(GetType(System.Windows.HorizontalAlignment))
Reference the
ObjectDataProvider
resource. The following XAML lists the enumeration values in a ListBox control:<ListBox Name="myComboBox" SelectedIndex="0" ItemsSource="{Binding Source={StaticResource EnumDataSource}}"/>
Full XAML
The following XAML code represents a simple window that does the following:
- Wraps the HorizontalAlignment enumeration in a ObjectDataProvider data source as a resource.
- Provides a ListBox control to list all enumeration values.
- Binds a Button control's HorizontalAlignment property to the selected item in the
ListBox
.
<Window x:Class="ArticleExample.BindEnumFull"
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="Enum binding">
<Window.Resources>
<ObjectDataProvider x:Key="EnumDataSource"
ObjectType="{x:Type sys:Enum}"
MethodName="GetValues">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="HorizontalAlignment" />
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</Window.Resources>
<StackPanel Width="300" Margin="10">
<TextBlock>Choose the HorizontalAlignment value of the Button:</TextBlock>
<ListBox Name="myComboBox" SelectedIndex="0"
ItemsSource="{Binding Source={StaticResource EnumDataSource}}"/>
<Button Content="I'm a button"
HorizontalAlignment="{Binding ElementName=myComboBox, Path=SelectedItem}" />
</StackPanel>
</Window>
See also
.NET Desktop feedback
Feedback
Submit and view feedback for