WPF:MenuItem.CommandParameter 绑定设置为 null [英] WPF : MenuItem.CommandParameter binding set to null

查看:64
本文介绍了WPF:MenuItem.CommandParameter 绑定设置为 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have the following ContextMenu defined for my data grid:

<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>

CommandViewModel 类定义如下:

A CommandViewModel class is defined as follows:

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 为空.

When I right click on a row in the grid, each MenuItem of the ContextMenu is correctly styled. The icon, label and command of the MenuItem is as expected. However, the command parameter, CommandViewModel.CommandParameter, that should be passed as argument to the RelayCommand bound to MenuItem.Command is null.

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

I am fairly certain that the command parameter available for the binding is not null. This is WPF application running on .NET 4.0.

有人经历过吗?

推荐答案

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

This is apparently a known problem with the CommandParameter binding.

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

Since I did not want to edit Prism code, I ended up using the CommandParameterBehavior class defined in the referenced CodePlex post.

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

Modifying my custom RelayCommand class to implement IDelegateCommand as follows:

 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);
    }      
}

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

and modifying my original style to use the CommandParameterBehavior like so:

 <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>

CommandParameter 现在已正确传递.

The CommandParameter is now passed correctly.

这篇关于WPF:MenuItem.CommandParameter 绑定设置为 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆