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.

Community
  • 1
  • 1
kevindaub
  • 3,293
  • 6
  • 35
  • 46

4 Answers4

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>
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
  • 1
    I just think he wants to binding the enum values as the itemssource of the menu. – Howard Apr 21 '11 at 15:20
  • 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. – myermian Apr 21 '11 at 19:42
  • I want to pass in the entire enumeration as an itemsource and the command parameter will be set to each enum in the enumeration. – kevindaub Apr 21 '11 at 22:07
  • Note: {x:Static local:TestEnum.First} <- this works at runtime and in the VS designer but does not in Blend 4. – Jack Ukleja Oct 07 '11 at 08:33
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.

myermian
  • 31,823
  • 24
  • 123
  • 215
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>
Howard
  • 3,638
  • 4
  • 32
  • 39
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.

H.B.
  • 166,899
  • 29
  • 327
  • 400