CommandBinding refresh on parameter changed I need the command I am executing on a datagrids context menu to know which column was clicked on.

XAML



&lt;DataGrid x:Name="dataGrid" ItemsSource="{Binding Table}" ContextMenuOpening="DataGrid_ContextMenuOpening"&gt;
    &lt;DataGrid.ContextMenu&gt;
        &lt;ContextMenu&gt;
            &lt;MenuItem Header="Add Column" Command="{Binding AddColumn}"/&gt;
            &lt;MenuItem Header="Remove Column" Command="{Binding RemoveColumn}"/&gt;
        &lt;/ContextMenu&gt;
    &lt;/DataGrid.ContextMenu&gt;

&lt;/DataGrid&gt;


Code behind:



private void DataGrid_ContextMenuOpening(object sender, ContextMenuEventArgs e)
{
    var current = e.OriginalSource as DependencyObject;
    while(current != null &amp;&amp; !(current is DataGridCell))
    {
        current = VisualTreeHelper.GetParent(current);
    }
    var cell = current as DataGridCell;
    foreach (var item in dataGrid.ContextMenu.Items.OfType&lt;MenuItem&gt;())
    {
        item.CommandParameter = cell.Column.DisplayIndex;
        (item.Command as DelegateCommand&lt;object&gt;)?.RaiseCanExecuteChanged();
    }
}


I'm having to break MVVM by having the view tell the VM command that its execution has changed because the command binding doesn't seem to refresh on the parameter changing. Is there a better way to do this?
I'm having to break MVVM by having the view tell the VM command that its execution has changed because the command binding doesn't seem to refresh on the parameter changing.


The fundamental problem here seems to be that MenuItem tests whether (in effect) Command.CanExecute(CommandParameter) to update its own Enabled property when Command changes, but not when CommandParameter changes.

It seems to me that the least inelegant way of working around this (IMO) bug in WPF is to subclass MenuItem and use DependencyProperty.OverrideMetadata to register a callback on CommandParameterProperty which updates an overridden IsEnabledCore property. To really do it properly you'd want to also override metadata for CommandTargetProperty and handle RoutedCommand specially, as in CommandHelpers.CanExecuteCommandSource.

An alternative approach, which would be hackier and require more XAML but wouldn't get quite so deep into the inner workings of WPF, would be an IMultiValueConverter which takes two parameters (the command and the parameter) and returns a parameterless command. Then you could set up multibindings such that a change in the parameter causes the MenuItem's Command to change, triggering its CanExecute test.

Both of these approaches would require changes in the XAML of all the grids, but no per-grid codebehind. They would also eliminate the assumption, which IMO is one of the nastiest problems of the codebehind in the question, that all commands are instances of DelegateCommand&lt;object&gt;.

(The other nasty problem is that if the while loop is broken because current == null, the body of the second loop will throw a NullReferenceException).