0

How could i bind all context menu items to one single command, which gets the index of the menu item called? I have a WPF/MVVM project.

<ContextMenu x:Key="cm" ItemsSource="{Binding ActionItems}" 
DisplayMemberPath="ActionDescription">    
</ContextMenu>
andy
  • 5,979
  • 2
  • 27
  • 49
relapse
  • 31
  • 2
  • 6

1 Answers1

1

Your should pass it as a CommandParameter:

<ContextMenu x:Key="cm" ItemsSource="{Binding ActionItems}" DisplayMemberPath="ActionDescription"
Command="{Binding YourCommand}" CommandParameter="{Binding YourParameter}">
...
</ContextMenu>  

Updated you need the solution described here: MVVM binding command to contextmenu item

Updated after comments

relapse - than you should implement it as shown here: WPF ContextMenu with ItemsSource - how to bind to Command in each item?. Please notice that link is a duplication of other question. So read the both please.

Community
  • 1
  • 1
MikroDel
  • 6,705
  • 7
  • 39
  • 74
  • I don't have those tags within the ContextMenu block! – relapse Jan 30 '13 at 12:39
  • Which "those tags" you mean? – MikroDel Jan 30 '13 at 12:40
  • Sorry, my fault, I've given too little information. I can't set those properties for each context menu item explicitly because the items are taken from an ObservableCollection - the list of them is dynamic. – relapse Jan 30 '13 at 12:54
  • I've tried this variant but it doesn't execute the command: `code` – relapse Jan 30 '13 at 16:11