Skip to main content
added 1309 characters in body
Source Link
Thomas Levesque
  • 293.7k
  • 73
  • 639
  • 769

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> 

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

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

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> 
Source Link
Thomas Levesque
  • 293.7k
  • 73
  • 639
  • 769

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

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