I have the TabControl on which I am setting the ContextMenu.
All ContextMenu elements have commands set.
<ContextMenu x:Key="tabMenu">
<MenuItem Command="{x:Static tabs:TabCommands.Close}" />
<MenuItem Command="{x:Static tabs:TabCommands.CloseAllButThis}" />
<MenuItem Command="{x:Static tabs:TabCommands.CloseAll}" />
</ContextMenu>
All commands are routed, and CommandBindings are defined several levels above the TabControl.
So the question is: in the CommandBinding CanExecute/Execute event handlers, what is the correct way to find out on which TabItem the menu was invoked? By correct I mean the one that would not broke if I change something like the TabItem template.
Or may be the whole approach is wrong and I should not use routed commands for this? I have originally used routing for Add New Tab command which requires hotkeys.
Thanks in advance.
UPDATE:
Igor's solution is cleaner from architectural POV (except that I would remove _ in ViewModel), but I want to have a reusable Close command that is independent from what TabControl is bound to (since Close/Close All for tabs exist in all kinds of applications and aren't semantically linked to a specific model).
Also, I can not use custom DataTemplate since I already have a custom template, and subclassing it would make solution a bit overcomplicated.