If the command is in the list item view model, then nothing needs to be done - the binding works correctly. MenuItemClick will be called in the class for which the list item is drawn.
If the command handler is in the view model that contains the list itself, then it’s impossible to point to the right place due to the fact that the context menu is not part of the logical tree and RelativeSource does not work.
To indicate the correct command, use various tricks like https://stackoverflow.com/questions/15033522/wpf-contextmenu-woes-how-do-set-the-datacontext-of-the-contextmenu
Instead of dancing with a tambourine, I use the CommandReference class.
public class CommandReference : Freezable, ICommand { public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof (ICommand), typeof (CommandReference ), new PropertyMetadata( OnCommandChanged)); public ICommand Command { get { return (ICommand) GetValue(CommandProperty); } set { SetValue(CommandProperty, value); } } private static void OnCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var commandReference = d as CommandReference; var oldCommand = e.OldValue as ICommand; var newCommand = e.NewValue as ICommand; if (oldCommand != null) { oldCommand.CanExecuteChanged -= commandReference.CanExecuteChanged; } if (newCommand != null) { newCommand.CanExecuteChanged += commandReference.CanExecuteChanged; } } #region Freezable protected override Freezable CreateInstanceCore() { return this; throw new NotImplementedException(); } #endregion #region ICommand Members public bool CanExecute(object parameter) { if (Command != null) return Command.CanExecute(parameter); return false; } public void Execute(object parameter) { Command.Execute(parameter); } public event EventHandler CanExecuteChanged; #endregion }
Which allows you to set a link to the command in the window / control resources
<mvvm:CommandReference x:Key="ShowCommandRef" Command="{Binding ShowCommand}" />
that is, relative to the DataContext window and call it in the menu
<MenuItem Command="{StaticResource ShowCommandRef}" CommandParameter="{Binding}" />