Passing Enum Value as a Command Parameter
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 开发者_StackOverflow社区of first, second, and third.
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>
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.
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.
<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>
精彩评论