diff --git a/dotnet-desktop-guide/net/wpf/data/how-to-bind-to-an-enumeration.md b/dotnet-desktop-guide/net/wpf/data/how-to-bind-to-an-enumeration.md new file mode 100644 index 0000000..3e42906 --- /dev/null +++ b/dotnet-desktop-guide/net/wpf/data/how-to-bind-to-an-enumeration.md @@ -0,0 +1,64 @@ +--- +title: How to bind to an enumeration +description: Learn how to use data binding to bind an enumeration to a collection object in XAML and in code for Windows Presentation Foundation. +author: adegeo +ms.author: adegeo +ms.date: 04/30/2021 +dev_langs: + - "csharp" + - "vb" +helpviewer_keywords: + - "binding data [WPF], enumeration" + - "data binding [WPF], enumeration" + - "enumeration [WPF]" +--- + +# 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 method returns a collection of values. These values can be wrapped in an and used as a data source. + +The type provides a convenient way to create an object in XAML and use it as a data source. + +[!INCLUDE [desktop guide under construction](../../includes/desktop-guide-preview-note.md)] + +## Reference the enumeration + +Use the type to wrap an array of enumeration values provided by the enumeration type itself. + +01. 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 the `ObjectDataProvider` with a resource key of `EnumDataSource`. + + :::code language="xaml" source="./snippets/how-to-bind-to-an-enumeration/csharp/BindEnum.xaml" id="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, . The `sys:` XAML namespace is mapped to `System`. | + | `MethodName` | The name of the method to run on the `System.Enum` type. In this example, . | + | `MethodParameters` | A collection of values to provide to the `MethodName` method. In this example, the method takes the `System.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: + + :::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"::: + +02. Reference the `ObjectDataProvider` resource. The following XAML lists the enumeration values in a 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: + +01. Wraps the enumeration in a data source as a resource. +01. Provides a control to list all enumeration values. +01. Binds a control's property to the selected item in the `ListBox`. + +:::code language="xaml" source="./snippets/how-to-bind-to-an-enumeration/csharp/BindEnumFull.xaml"::: + +## See also + +- [Data binding overview](index.md) +- [Binding sources overview](binding-sources-overview.md) +- [StaticResource Markup Extension](../../../framework/wpf/advanced/staticresource-markup-extension.md) +- [An alternative way to bind to an enumeration](https://brianlagunas.com/a-better-way-to-data-bind-enums-in-wpf/) diff --git a/dotnet-desktop-guide/net/wpf/data/snippets/how-to-bind-to-an-enumeration/csharp/App.xaml b/dotnet-desktop-guide/net/wpf/data/snippets/how-to-bind-to-an-enumeration/csharp/App.xaml new file mode 100644 index 0000000..f0ef078 --- /dev/null +++ b/dotnet-desktop-guide/net/wpf/data/snippets/how-to-bind-to-an-enumeration/csharp/App.xaml @@ -0,0 +1,9 @@ + + + + + diff --git a/dotnet-desktop-guide/net/wpf/data/snippets/how-to-bind-to-an-enumeration/csharp/App.xaml.cs b/dotnet-desktop-guide/net/wpf/data/snippets/how-to-bind-to-an-enumeration/csharp/App.xaml.cs new file mode 100644 index 0000000..2e47dea --- /dev/null +++ b/dotnet-desktop-guide/net/wpf/data/snippets/how-to-bind-to-an-enumeration/csharp/App.xaml.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Data; +using System.Linq; +using System.Threading.Tasks; +using System.Windows; + +namespace ArticleExample +{ + /// + /// Interaction logic for App.xaml + /// + public partial class App : Application + { + } +} diff --git a/dotnet-desktop-guide/net/wpf/data/snippets/how-to-bind-to-an-enumeration/csharp/ArticleExample.csproj b/dotnet-desktop-guide/net/wpf/data/snippets/how-to-bind-to-an-enumeration/csharp/ArticleExample.csproj new file mode 100644 index 0000000..407932c --- /dev/null +++ b/dotnet-desktop-guide/net/wpf/data/snippets/how-to-bind-to-an-enumeration/csharp/ArticleExample.csproj @@ -0,0 +1,9 @@ + + + + WinExe + net5.0-windows + true + + + diff --git a/dotnet-desktop-guide/net/wpf/data/snippets/how-to-bind-to-an-enumeration/csharp/AssemblyInfo.cs b/dotnet-desktop-guide/net/wpf/data/snippets/how-to-bind-to-an-enumeration/csharp/AssemblyInfo.cs new file mode 100644 index 0000000..2211234 --- /dev/null +++ b/dotnet-desktop-guide/net/wpf/data/snippets/how-to-bind-to-an-enumeration/csharp/AssemblyInfo.cs @@ -0,0 +1,10 @@ +using System.Windows; + +[assembly:ThemeInfo( + ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located + //(used if a resource is not found in the page, + // or application resource dictionaries) + ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located + //(used if a resource is not found in the page, + // app, or any theme specific resource dictionaries) +)] diff --git a/dotnet-desktop-guide/net/wpf/data/snippets/how-to-bind-to-an-enumeration/csharp/BindEnum.xaml b/dotnet-desktop-guide/net/wpf/data/snippets/how-to-bind-to-an-enumeration/csharp/BindEnum.xaml new file mode 100644 index 0000000..ca1ccfe --- /dev/null +++ b/dotnet-desktop-guide/net/wpf/data/snippets/how-to-bind-to-an-enumeration/csharp/BindEnum.xaml @@ -0,0 +1,31 @@ + + + + + + + + + + + + + Choose the HorizontalAlignment value of the Button: + + + + + + + + diff --git a/dotnet-desktop-guide/net/wpf/data/snippets/how-to-bind-to-an-enumeration/csharp/MainWindow.xaml.cs b/dotnet-desktop-guide/net/wpf/data/snippets/how-to-bind-to-an-enumeration/csharp/MainWindow.xaml.cs new file mode 100644 index 0000000..29dcc8c --- /dev/null +++ b/dotnet-desktop-guide/net/wpf/data/snippets/how-to-bind-to-an-enumeration/csharp/MainWindow.xaml.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Navigation; +using System.Windows.Shapes; + +namespace ArticleExample +{ + /// + /// Interaction logic for MainWindow.xaml + /// + public partial class MainWindow : Window + { + public MainWindow() + { + InitializeComponent(); + } + + private void Button1_Click(object sender, RoutedEventArgs e) + { + new BindEnum().ShowDialog(); + } + } +} diff --git a/dotnet-desktop-guide/net/wpf/data/snippets/how-to-bind-to-an-enumeration/csharp/PrimaryColors.cs b/dotnet-desktop-guide/net/wpf/data/snippets/how-to-bind-to-an-enumeration/csharp/PrimaryColors.cs new file mode 100644 index 0000000..73330ad --- /dev/null +++ b/dotnet-desktop-guide/net/wpf/data/snippets/how-to-bind-to-an-enumeration/csharp/PrimaryColors.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ArticleExample +{ + enum PrimaryColors + { + Red, + Yellow, + Blue + } +} diff --git a/dotnet-desktop-guide/net/wpf/data/snippets/how-to-bind-to-an-enumeration/vb/Application.xaml b/dotnet-desktop-guide/net/wpf/data/snippets/how-to-bind-to-an-enumeration/vb/Application.xaml new file mode 100644 index 0000000..76d5b6a --- /dev/null +++ b/dotnet-desktop-guide/net/wpf/data/snippets/how-to-bind-to-an-enumeration/vb/Application.xaml @@ -0,0 +1,9 @@ + + + + + diff --git a/dotnet-desktop-guide/net/wpf/data/snippets/how-to-bind-to-an-enumeration/vb/Application.xaml.vb b/dotnet-desktop-guide/net/wpf/data/snippets/how-to-bind-to-an-enumeration/vb/Application.xaml.vb new file mode 100644 index 0000000..f3e8ffd --- /dev/null +++ b/dotnet-desktop-guide/net/wpf/data/snippets/how-to-bind-to-an-enumeration/vb/Application.xaml.vb @@ -0,0 +1,6 @@ +Class Application + + ' Application-level events, such as Startup, Exit, and DispatcherUnhandledException + ' can be handled in this file. + +End Class diff --git a/dotnet-desktop-guide/net/wpf/data/snippets/how-to-bind-to-an-enumeration/vb/ArticleExampleVB.vbproj b/dotnet-desktop-guide/net/wpf/data/snippets/how-to-bind-to-an-enumeration/vb/ArticleExampleVB.vbproj new file mode 100644 index 0000000..15b9652 --- /dev/null +++ b/dotnet-desktop-guide/net/wpf/data/snippets/how-to-bind-to-an-enumeration/vb/ArticleExampleVB.vbproj @@ -0,0 +1,22 @@ + + + + WinExe + net5.0-windows + ArticleExampleVB + true + + + + + + + + + + + + + + + diff --git a/dotnet-desktop-guide/net/wpf/data/snippets/how-to-bind-to-an-enumeration/vb/AssemblyInfo.vb b/dotnet-desktop-guide/net/wpf/data/snippets/how-to-bind-to-an-enumeration/vb/AssemblyInfo.vb new file mode 100644 index 0000000..025ee72 --- /dev/null +++ b/dotnet-desktop-guide/net/wpf/data/snippets/how-to-bind-to-an-enumeration/vb/AssemblyInfo.vb @@ -0,0 +1,11 @@ +Imports System.Windows + +'The ThemeInfo attribute describes where any theme specific and generic resource dictionaries can be found. +'1st parameter: where theme specific resource dictionaries are located +'(used if a resource is not found in the page, +' or application resource dictionaries) + +'2nd parameter: where the generic resource dictionary is located +'(used if a resource is not found in the page, +'app, and any theme specific resource dictionaries) + diff --git a/dotnet-desktop-guide/net/wpf/data/snippets/how-to-bind-to-an-enumeration/vb/BindEnum.xaml b/dotnet-desktop-guide/net/wpf/data/snippets/how-to-bind-to-an-enumeration/vb/BindEnum.xaml new file mode 100644 index 0000000..e18d268 --- /dev/null +++ b/dotnet-desktop-guide/net/wpf/data/snippets/how-to-bind-to-an-enumeration/vb/BindEnum.xaml @@ -0,0 +1,12 @@ + + + + + diff --git a/dotnet-desktop-guide/net/wpf/data/snippets/how-to-bind-to-an-enumeration/vb/BindEnum.xaml.vb b/dotnet-desktop-guide/net/wpf/data/snippets/how-to-bind-to-an-enumeration/vb/BindEnum.xaml.vb new file mode 100644 index 0000000..a4f8173 --- /dev/null +++ b/dotnet-desktop-guide/net/wpf/data/snippets/how-to-bind-to-an-enumeration/vb/BindEnum.xaml.vb @@ -0,0 +1,7 @@ +Public Class BindEnum + Public Sub Method1() + ' + Dim enumDataSource = System.Enum.GetValues(GetType(System.Windows.HorizontalAlignment)) + ' + End Sub +End Class diff --git a/dotnet-desktop-guide/net/wpf/data/snippets/how-to-bind-to-an-enumeration/vb/MainWindow.xaml b/dotnet-desktop-guide/net/wpf/data/snippets/how-to-bind-to-an-enumeration/vb/MainWindow.xaml new file mode 100644 index 0000000..9fc7d3a --- /dev/null +++ b/dotnet-desktop-guide/net/wpf/data/snippets/how-to-bind-to-an-enumeration/vb/MainWindow.xaml @@ -0,0 +1,12 @@ + + + + + diff --git a/dotnet-desktop-guide/net/wpf/data/snippets/how-to-bind-to-an-enumeration/vb/MainWindow.xaml.vb b/dotnet-desktop-guide/net/wpf/data/snippets/how-to-bind-to-an-enumeration/vb/MainWindow.xaml.vb new file mode 100644 index 0000000..21be82b --- /dev/null +++ b/dotnet-desktop-guide/net/wpf/data/snippets/how-to-bind-to-an-enumeration/vb/MainWindow.xaml.vb @@ -0,0 +1,7 @@ +Class MainWindow + Public Sub Method1() + ' + Dim enumValues = System.Enum.GetValues(GetType(System.Windows.HorizontalAlignment)) + ' + End Sub +End Class diff --git a/dotnet-desktop-guide/net/wpf/toc.yml b/dotnet-desktop-guide/net/wpf/toc.yml index 90f2d47..dd26e96 100644 --- a/dotnet-desktop-guide/net/wpf/toc.yml +++ b/dotnet-desktop-guide/net/wpf/toc.yml @@ -55,6 +55,11 @@ items: href: data/binding-declarations-overview.md - name: Binding sources href: data/binding-sources-overview.md + items: + - name: Common tasks + items: + - name: Bind to an enumeration + href: data/how-to-bind-to-an-enumeration.md - name: Systems expanded: true items: