WPF:MenuItem.CommandParameter绑定设置为null

Kla*_*Nji 5 c# wpf binding mvvm menuitem

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

<igDP:XamDataGrid.ContextMenu>
    <ContextMenu ItemsSource="{Binding CommandViewModels}"                     >
        <ContextMenu.ItemContainerStyle>
            <Style TargetType="MenuItem">
                <Setter Property="Command" Value="{Binding Command}" />
                <Setter Property="CommandParameter" Value="{Binding CommandParameter}" />
                <Setter Property="Header" Value="{Binding Name}" />
                <Setter Property="Icon" Value="{Binding Icon}" />
            </Style>
        </ContextMenu.ItemContainerStyle>
    </ContextMenu>
</igDP:XamDataGrid.ContextMenu>
Run Code Online (Sandbox Code Playgroud)

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; }     
    }
Run Code Online (Sandbox Code Playgroud)

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

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

任何人都经历过吗?

Kla*_*Nji 2

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

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

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

 public class RelayCommand : IDelegateCommand
{
    readonly protected Predicate<object> _canExecute;
    readonly protected Action<object> _execute;      

    public RelayCommand(Predicate<object> canExecute, Action<object> 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);
    }      
}
Run Code Online (Sandbox Code Playgroud)

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

 <Style TargetType="MenuItem">
                <Setter Property="Command" Value="{Binding Command}" />
                <Setter Property="CommandParameter" Value="{Binding CommandParameter}" />
                <Setter Property="Header" Value="{Binding Name}" />
                <Setter Property="Icon" Value="{Binding Icon}" />
                <Setter Property="utility:CommandParameterBehavior.IsCommandRequeriedOnChange" Value="true"
            </Style>
Run Code Online (Sandbox Code Playgroud)

CommandParameter 现在已正确传递。