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}}"/>
This is a
DevExpress
specific answer based on the top-voted answer byGregor S.
(currently it has 128 votes).This means we can keep the styling consistent across the entire application:
Unfortunately, the original answer doesn't work with a
ComboBoxEdit
from DevExpress without some modifications.First, the XAML for the
ComboBoxEdit
:Needsless to say, you will need to point
xamlExtensions
at the namespace that contains the XAML extension class (which is defined below):And we have to point
myEnum
at the namespace that contains the enum:Then, the enum:
The problem in with the XAML is that we can't use
SelectedItemValue
, as this throws an error as the setter is unaccessable (bit of an oversight on your part,DevExpress
). So we have to modify ourViewModel
to obtain the value directly from the object:For completeness, here is the XAML extension from the original answer (slightly renamed):
Disclaimer: I have no affiliation with DevExpress. Telerik is also a great library.
Use ObjectDataProvider:
and then bind to static resource:
Find this solution at this blog
Try using
I've created an open source CodePlex project that does this. You can download the NuGet package from here.
If you are using a MVVM, based on @rudigrobler answer you can do the following:
Add the following property to the ViewModel class
Then in the XAML do the following:
Here is a generic solution using a helper method. This can also handle an enum of any underlying type (byte, sbyte, uint, long, etc.)
Helper Method:
View Model:
ComboBox: