I have a context menu. It's bound to some collection and it has a defined ItemTemplate like this:
<ContextMenu
ItemsSource={Binding ...}
ItemTemplate={StaticResource itemTemplate}
/>
itemTemplate is a simple DataTemplate with a TextBlock:
<DataTemplate x:Key="itemTemplate">
<TextBlock Text={Binding ...} />
</DataTemplate>
How do I bind Command property for MenuItem to the underlying object's property?
I think you need to wrap your TextBlock in a MenuItem:
But I don't have an IDE in front of me right now to try this. Let me know how it goes.
Looks like you need to use the ItemContainerStyle as seen here. Sorry for leading you down the wrong path at the start there - but I got in front of an IDE and this works:
Although this is only a slight variation on Martin Harris's answer, I thought I'd share it anyway. I found it more useful specify a single command for the whole collection and also send along a CommandParameter:
Then you can determine what to do in the handler for the command:
I realize I'm answering this quite a bit after the fact, but I ran into the same problem and the previous answers seemed to make binding to multiple different commands difficult. The solution I arrived is very similar to MatrixManAtYrService's and works in 3 parts:
1) Bind the command using the ItemContainerStyle property to a command in the ViewModel -- this is the same as the previous answers. One exception is that I bind the CommandParameter to the MenuItem.
2) Create a custom class to define the look and behavior of each MenuItem. The ItemsSource of the menu will be set to a list of these. This is the same as other answers. However, in my implementation I have given the class an Action to be executed when the MenuItemCommand is invoked. I also included a boolean that will allow the MenuItem to be disabled.
3) In the command implementation route control to the delegates in MenuAction.
This should allow you to define all of the behavior of your commands in a list. While it is technically binding to a single command it is functionally similar to having multiple different commands. The same method should also work with nested MenuItems and HierarchicalDataTemplates with some tweaking.