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

查看:1556
本文介绍了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; }     
    }

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

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.

我相当肯定,可用于绑定的命令参数不为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.

任何人都经历过这样?

Anyone experienced this?

推荐答案

这显然是一个已知的问题CommandParameter结合。

This is apparently a known problem with the CommandParameter binding.

由于我没有想编辑棱镜code,我结束了使用 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天全站免登陆