6

Part of this question has been answered on how to bind to an enum as a command parameter, but I need to go one step further.

I have a data template that links to a menu and each menu option initiates a command with a different value of the enum. How do I do this? Do I need to resort to just passing a string?

public enum TestEnum { First, Second, Third } 
<DataTemplate> <MenuItem Header="{Binding Path=.}" Command="{Binding ACommand}" CommandParameter="{Binding Path=???}" /> </DataTemplate> 

I want the first MenuItem to bind to Enum.First, the second one to Enum.Second, and so on. I want this written, so that I only have to write the data template above once within a Menu instead of a menu item for each enum.value. HTH.

I need the command parameter to be different for each menu item. So I will have 3 menu items of first, second, and third.

3
  • What is the ItemsSource that you bind to? Commented Apr 21, 2011 at 14:48
  • Do you want to binding the enum as itemssource for the menu? And set each menuItem as the enum item? Commented Apr 21, 2011 at 14:49
  • My ItemsSource is a list of the values. I want to bind the enum as the itemsSource with each menu Item having an enum. Commented Apr 21, 2011 at 14:53

4 Answers 4

10

Not sure I understand your requirement correctly... is this what you want?

CommandParameter="{Binding Path={x:Static local:TestEnum.First}}" 

EDIT: ok, I think I understand now... If you want the enum values as the ItemsSource, you could do it with an ObjectDataProvider, but there's a better way: write a markup extension that takes in the type of the enum and returns the values.

Markup extension

[MarkupExtensionReturnType(typeof(Array))] public class EnumValuesExtension : MarkupExtension { public EnumValuesExtension() { } public EnumValuesExtension(Type enumType) { this.EnumType = enumType; } [ConstructorArgument("enumType")] public Type EnumType { get; set; } public override object ProvideValue(IServiceProvider serviceProvider) { return Enum.GetValues(EnumType); } } 

XAML

<MenuItem ItemsSource="{my:EnumValues EnumType=my:TestEnum}" Name="menu"> <MenuItem.ItemContainerStyle> <Style TargetType="MenuItem"> <Setter Property="Header" Value="{Binding}" /> <Setter Property="Command" Value="{Binding SomeCommand, ElementName=menu}" /> <Setter Property="CommandParameter" Value="{Binding}" /> </Style> </MenuItem.ItemContainerStyle> </MenuItem> 
Sign up to request clarification or add additional context in comments.

4 Comments

I just think he wants to binding the enum values as the itemssource of the menu.
I think you're answer is close, except if I had to guess, I'd say that daub815 intends to pass in a predetermined enum value for each of his MenuItems.
I want to pass in the entire enumeration as an itemsource and the command parameter will be set to each enum in the enumeration.
Note: {x:Static local:TestEnum.First} <- this works at runtime and in the VS designer but does not in Blend 4.
6

If you want to pass a predetermined Enum value (which it sounds like you do) for the MenuItem you'd do it like so... (make sure to import the xmlns:local="..." in your xaml as well)

<MenuItem ... CommandParameter="{x:Static local:TestEnum.First}" /> 

You don't need to actually bind to anything for the CommandParameter in the instance that you're asking (I think). Binding a value to the CommandParameter implies that the value of the CommandParameter can vary and the source of that value is contained somewhere else, either as a value on another element's DepenedencyProperty or a CLR value within a DataContext.

Comments

1

I want to bind the enum as the itemsSource with each menu Item having an enum.

If your ItemsSource is the enum itself you can just write CommandParameter="{Binding}" and it will pass the current enum value.

Comments

1
<ObjectDataProvider ObjectType="{x:Type sys:Enum}" MethodName="GetValues" x:Key="EnumProvider"> <ObjectDataProvider.MethodParameters> <x:TypeExtension Type="{x:Type local:TestEnum}" /> </ObjectDataProvider.MethodParameters> </ObjectDataProvider> <!--Binding the resource as ItemsSource--> <Menu ItemsSource="{Binding Source={StaticResource EnumProvider}}" /> 

or

<Menu> <MenuItem Header="TestEnum" ItemsSource="{Binding Source={StaticResource EnumProvider}}" > <MenuItem.ItemTemplate> <DataTemplate> <MenuItem Header="{Binding Path=.}" Command="{Binding ACommand}" CommandParameter="{Binding Path=???}" /> </DataTemplate> </MenuItem.ItemTemplate> </MenuItem> </Menu> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.