Passing an Enum value as a command parameter

Partially this question was answered how to connect with enum as a parameter of the command , but I need to take one more step.

I have a data template that references a menu, and each menu item launches a command with a different enumeration value. How can I do it? Do I need to resort to a simple line break?

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

I want the first MenuItem to communicate with Enum.First, the second - Enum.Second, etc. I want this to be written, so I only need to write the data template above once in the menu instead of the menu item for each enum.value. NTN.

I need a command parameter for different menu items. Therefore, I will have 3 menu items of the first, second and third.

+6
source share
4 answers

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

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

EDIT: OK, I think now I understand ... If you want the enumeration values ​​to be an ItemsSource , you could do it with an ObjectDataProvider , but there is a better way: write a markup extension that takes the enumeration type 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> 
+10
source

If you want to pass the value of the predefined Enum (which you think) to MenuItem, you would do it like that ... (be sure to import xmlns:local="..." into your xaml as well)

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

You do not need to impose anything on the CommandParameter in the instance you specify (I think). Binding a value to a CommandParameter means that the value of the CommandParameter parameter can change, and the source of this value is stored somewhere else, either as a value for another DepenedencyProperty element, or as a CLR value in a DataContext.

+5
source
 <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> 
+1
source

I want to associate enum as itemsSource with each menu item with an enumeration.

If your ItemSource is an enumeration, you can simply write CommandParameter="{Binding}" and pass the current value of the enumeration.

+1
source

Source: https://habr.com/ru/post/886402/


All Articles