WPF:MenuItem.CommandParameter绑定设置为null

我为我的数据网格定义了以下ContextMenu:

            

CommandViewModel类定义如下:

 public class CommandViewModel : ICommandViewModel { public CommandViewModel(string name, Image icon, ICommand command, object commandParameter = null, int index = 0) { Name = name; Icon = icon; Command = command; CommandParameter = commandParameter; Index = index; } public string Name { get; set; } public Image Icon { get; set; } public ICommand Command { get; set; } public object CommandParameter { get; set; } public int Index { get; set; } } 

当我右键单击网格中的一行时,ContextMenu的每个MenuItem都被正确设置样式。 MenuItem的图标,标签和命令是预期的。 但是,应作为参数传递给绑定到MenuItem.Command的RelayCommand的命令参数CommandViewModel.CommandParameter为null。

我很确定可用于绑定的命令参数不为null。 这是在.NET 4.0上运行的WPF应用程序。

谁经历过这个?

这显然是CommandParameter绑定的已知问题。

由于我不想编辑Prism代码,我最终使用了引用的CodePlexpost中定义的CommandParameterBehavior类。

修改我的自定义RelayCommand类以实现IDelegateCommand,如下所示:

  public class RelayCommand : IDelegateCommand { readonly protected Predicate _canExecute; readonly protected Action _execute; public RelayCommand(Predicate canExecute, Action execute) { _canExecute = canExecute; _execute = execute; } public void RaiseCanExecuteChanged() { if (CanExecuteChanged != null) CanExecuteChanged(this, EventArgs.Empty); } public virtual bool CanExecute(object parameter) { return _canExecute(parameter); } public event EventHandler CanExecuteChanged; public virtual void Execute(object parameter) { _execute(parameter); } } 

并修改我的原始样式以使用CommandParameterBehavior,如下所示:

   

CommandParameter现在正确传递。