As an example take the following code:
public enum ExampleEnum { FooBar, BarFoo }
public class ExampleClass : INotifyPropertyChanged
{
private ExampleEnum example;
public ExampleEnum ExampleProperty
{ get { return example; } { /* set and notify */; } }
}
I want a to databind the property ExampleProperty to a ComboBox, so that it shows the options "FooBar" and "BarFoo" and works in mode TwoWay. Optimally I want my ComboBox definition to look something like this:
<ComboBox ItemsSource="What goes here?" SelectedItem="{Binding Path=ExampleProperty}" />
Currently I have handlers for the ComboBox.SelectionChanged and ExampleClass.PropertyChanged events installed in my Window where I do the binding manually.
Is there a better or some kind of canonical way? Would you usually use Converters and how would you populate the ComboBox with the right values? I don't even want to get started with i18n right now.
Edit
So one question was answered: How do I populate the ComboBox with the right values.
Retrieve Enum values as a list of strings via an ObjectDataProvider from the static Enum.GetValues method:
<Window.Resources>
<ObjectDataProvider MethodName="GetValues"
ObjectType="{x:Type sys:Enum}"
x:Key="ExampleEnumValues">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="ExampleEnum" />
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</Window.Resources>
This I can use as an ItemsSource for my ComboBox:
<ComboBox ItemsSource="{Binding Source={StaticResource ExampleEnumValues}}"/>
You can create a custom markup extension.
Example of usage:
At the top of your XAML:
and then...
And the implementation...
you can consider something like that:
define a style for textblock, or any other control you want to use to display your enum:
define your style for ComboBoxItem
add a combobox and load it with your enum values:
if your enum is large, you can of course do the same in code, sparing a lot of typing. i like that approach, since it makes localization easy - you define all the templates once, and then, you only update your string resource files.
In the viewmodel you can have:
In XAML the ItemSource binds to MyEnumTypeValues and SelectedItem binds to SelectedMyEnumType.
I prefer not to use the name of enum in UI. I prefer use different value for user (
DisplayMemberPath
) and different for value (enum in this case) (SelectedValuePath
). Those two values can be packed toKeyValuePair
and stored in dictionary.XAML
C#
EDIT: Compatible with the MVVM pattern.
I don't know if it is possible in XAML-only but try the following:
Give your ComboBox a name so you can access it in the codebehind: "typesComboBox1"
Now try the following
Based on the accepted but now deleted answer provided by ageektrapped I created a slimmed down version without some of the more advanced features. All the code is included here to allow you to copy-paste it and not get blocked by link-rot.
I use the
System.ComponentModel.DescriptionAttribute
which really is intended for design time descriptions. If you dislike using this attribute you may create your own but I think using this attribute really gets the job done. If you don't use the attribute the name will default to the name of the enum value in code.Here is the class used as the items source:
You can use it in XAML like this: