I have an ObjectDataProvider for getting a list of enum members:
<ObjectDataProvider x:Key="GetEnumContents" MethodName="GetValues" ObjectType="{x:Type System:Enum}">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="Data:Status"/>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
which I then use with:
<ComboBox SelectedItem="{Binding Status, UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding Source={StaticResource GetEnumContents}}" />
In the same window, I'd then like to have a combo box for a different enum. How do I pass the enum type from the ComboBox declaration?
I've seen solutions to similar problems something like:
Path="MethodParameters[0]"
but here I don't want to bind the parameter to anything, I just want to hard-code it in the ComboBox declaration.
Any ideas?
ObjectDataProviderdoesn't support that kind of functionality, but you can "fake" it with the cleverabuseusage of aBindingand anIValueConverter.First, the
IValueConverter:Here is how you use it:
Some Test Enums:
This produces the following output:

This takes advantage of the fact that you can pass an extra parameter to an
IValueConverter, which I use to pass theTypeof the enumeration to the converter. The converter just callsEnum.GetNameson that argument, and returns the result. The actualBindingwill actually be bound to whatever theDataContextof theComboBoxhappens to be. TheEnumConverterjust happily ignores it and operates on the parameter instead.UPDATE
It works even better by binding directly to the type, skipping the
ConverterParameterentirely, like so:With adjustments to the converter:
Same result with less typing, and easier to understand, code.