【问题标题】:WPF : MenuItem.CommandParameter binding set to nullWPF:MenuItem.CommandParameter 绑定设置为 null
【发布时间】:2013-12-18 23:46:26
【问题描述】:

我为我的数据网格定义了以下 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>

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。

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

有人经历过吗?

【问题讨论】:

  • 你有多少把握,你调试过代码吗?
  • 我刚刚尝试了您的确切代码,并且参数通过了...可能是您的 ICommand 实现缺少接收参数的功能吗?
  • @Tico,我分配给 CommandParameter 的值不为空。此外,如果我修改 CommandParameter 以便可以在其 getter 中放置一个断点,则框架检索到的值不为 null。
  • @emedbo,我之前在 TreeView 上也使用过相同的代码,所以它可能是 XamDataGrid 的特殊之处。
  • 即使尝试以编程方式创建 MenuItems 并且传递给 RelayCommand 的 CanExecute 的命令参数仍然为空。非常有趣的问题。

标签: c# wpf mvvm binding menuitem


【解决方案1】:

这显然是 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);
    }      
}

并修改我的原始样式以使用 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>

CommandParameter 现在已正确传递。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-27
    • 2016-02-07
    • 2020-01-31
    相关资源
    最近更新 更多