* add how to article * Fix lint * Fix the code example * Add missing code * Update how-to-bind-to-an-enumeration.md * Update dotnet-desktop-guide/net/wpf/data/how-to-bind-to-an-enumeration.md Co-authored-by: Tom Dykstra <[email protected]> Co-authored-by: Tom Dykstra <[email protected]>
4.2 KiB
title, description, author, ms.author, ms.date, dev_langs, helpviewer_keywords
| title | description | author | ms.author | ms.date | dev_langs | helpviewer_keywords | |||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| How to bind to an enumeration | Learn how to use data binding to bind an enumeration to a collection object in XAML and in code for Windows Presentation Foundation. | adegeo | adegeo | 04/30/2021 |
|
|
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 xref:System.Enum.GetValues(System.Type)?displayProperty=nameWithType method returns a collection of values. These values can be wrapped in an xref:System.Windows.Data.ObjectDataProvider and used as a data source.
The xref:System.Windows.Data.ObjectDataProvider type provides a convenient way to create an object in XAML and use it as a data source.
[!INCLUDE desktop guide under construction]
Reference the enumeration
Use the xref:System.Windows.Data.ObjectDataProvider type to wrap an array of enumeration values provided by the enumeration type itself.
-
Create a new
ObjectDataProvideras 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 theObjectDataProviderwith a resource key ofEnumDataSource.:::code language="xaml" source="./snippets/how-to-bind-to-an-enumeration/csharp/BindEnum.xaml" id="Resources":::
In this example, the
ObjectDataProvideruses three properties to retrieve the enumeration:Property Description ObjectTypeThe type of object to be returned by the data provider. In this example, xref:System.Enum?displayProperty=fullName. The sys:XAML namespace is mapped toSystem.MethodNameThe name of the method to run on the System.Enumtype. In this example, xref:System.Enum.GetValues%2A?displayProperty=nameWithType.MethodParametersA collection of values to provide to the MethodNamemethod. In this example, the method takes theSystem.Typeof the enumeration.Effectively, the XAML is breaking down a method call, method name, parameters, and the return type. The
ObjectDataProviderconfigured in the previous example is the equivalent of the following code::::code language="csharp" source="./snippets/how-to-bind-to-an-enumeration/csharp/BindEnum.xaml.cs" id="EnumGetValues"::: :::code language="vb" source="./snippets/how-to-bind-to-an-enumeration/vb/BindEnum.xaml.vb" id="EnumGetValues":::
-
Reference the
ObjectDataProviderresource. The following XAML lists the enumeration values in a xref:System.Windows.Controls.ListBox control::::code language="xaml" source="./snippets/how-to-bind-to-an-enumeration/csharp/BindEnum.xaml" id="ListBox":::
Full XAML
The following XAML code represents a simple window that does the following:
- Wraps the xref:System.Windows.HorizontalAlignment enumeration in a xref:System.Windows.Data.ObjectDataProvider data source as a resource.
- Provides a xref:System.Windows.Controls.ListBox control to list all enumeration values.
- Binds a xref:System.Windows.Controls.Button control's xref:System.Windows.FrameworkElement.HorizontalAlignment property to the selected item in the
ListBox.
:::code language="xaml" source="./snippets/how-to-bind-to-an-enumeration/csharp/BindEnumFull.xaml":::