为什么绑定到 RelayCommand 的 ContextMenu 项始终被禁用?

发布于 2024-11-30 23:08:02 字数 1943 浏览 5 评论 0原文

我现在正在开发我的第一个大型 WPF MVVM 应用程序,该应用程序将 MVVM Light Toolkit 与 Josh Smith 的 RelayCommand 结合使用。 我遇到的问题是我将此命令绑定到 ContextMenu 中的一个项目,该项目始终保持禁用状态。

这是 MenuItem 的代码片段:

<MenuItem
    Header="Verwijderen"
    Command="{StaticResource DeleteNoteCommandReference}"
    CommandParameter="{Binding}" />

现在我对命令绑定所做的操作如下: 我使用了一个名为 CommandReference 的类,我发现它 此处

这是命令引用本身:

<command:CommandReference
    x:Key="DeleteNoteCommandReference"
    Command="{Binding DeleteNoteCommand}" />

我这样做的原因是因为我注意到 ContextMenu 上的命令绑定存在问题(由于 ContextMenu 不是逻辑/可视树的一部分)。我在网上找到了几个关于这个主题的主题,在其中一些主题中我发现了 CommandReference 类,这似乎是解决我的问题的一个很好的解决方案。 这些命令绑定问题确实消失了,但似乎我的命令的 CanExecute 无法识别,或者因为 MenuItem 保持禁用状态。

在 ViewModel(作为其 DataContext 绑定到视图)中,我有以下命令代码:

    /// <summary>
    /// Command for deleting a note.
    /// </summary>
    public RelayCommand<NoteViewModel> DeleteNoteCommand {
        get;
        private set;
    }

    /// <summary>
    /// CanExecute method for the DeleteNoteCommand.
    /// </summary>
    /// <param name="note">The NoteViewModel that CanExecute needs to check.</param>
    /// <returns>True if the note can be deleted, false otherwise.</returns>
    public bool DeleteNoteCommandCanExecute(NoteViewModel note) {
        return Notes.Contains(note);
    }

    /// <summary>
    /// Creates all commands for this ViewModel.
    /// </summary>
    private void CreateCommands() {
        DeleteNoteCommand = new RelayCommand<NoteViewModel>(param => DeleteNote(param), param => DeleteNoteCommandCanExecute(param));
    }

为了让我的代码发挥作用,我在这里缺少什么? 我认为这可能与我正在使用的 CommandReference 有关,但我不知道要寻找什么。

真心希望大家能够帮忙!

I'm working on my first big WPF MVVM application now, which uses MVVM Light Toolkit in combination with Josh Smith's RelayCommand.
The problem I run into is that I bound this command to an item in a ContextMenu, which keeps disabled all the time.

Here's a code snippet of the MenuItem:

<MenuItem
    Header="Verwijderen"
    Command="{StaticResource DeleteNoteCommandReference}"
    CommandParameter="{Binding}" />

Now what I've done with the commandbinding is the following: I used a class called CommandReference which I found here.

This is the commandreference itself:

<command:CommandReference
    x:Key="DeleteNoteCommandReference"
    Command="{Binding DeleteNoteCommand}" />

The reason why I've done this is because of problems with commandbinding on a ContextMenu that I noticed (caused by the fact that a ContextMenu is not part of the logical/visual tree). I found several topics about this subject on the net and in some of them I spotted the CommandReference class, which seemed a good solution to my problem.
These commandbinding issues where indeed gone but it seems that the CanExecute of my command isn't recognized or something because the MenuItem keeps disabled.

In the ViewModel (which is bound to the view as its DataContext), I have the following code for the command:

    /// <summary>
    /// Command for deleting a note.
    /// </summary>
    public RelayCommand<NoteViewModel> DeleteNoteCommand {
        get;
        private set;
    }

    /// <summary>
    /// CanExecute method for the DeleteNoteCommand.
    /// </summary>
    /// <param name="note">The NoteViewModel that CanExecute needs to check.</param>
    /// <returns>True if the note can be deleted, false otherwise.</returns>
    public bool DeleteNoteCommandCanExecute(NoteViewModel note) {
        return Notes.Contains(note);
    }

    /// <summary>
    /// Creates all commands for this ViewModel.
    /// </summary>
    private void CreateCommands() {
        DeleteNoteCommand = new RelayCommand<NoteViewModel>(param => DeleteNote(param), param => DeleteNoteCommandCanExecute(param));
    }

What am I missing here to get my code functional?
I thought it might have something to do with the CommandReference I'm using, but I don't know what to look for.

Really hope you guys can help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悲歌长辞 2024-12-07 23:08:02

尝试在 DeleteNoteCommandCanExecute 内部设置断点并检查:

  1. 是否在打开上下文菜单之前调用它,并且
  2. DeleteNoteCommandCanExecute 内部的代码不会引发异常(例如 注意参数为空)

在第一种情况下,如果没有被调用,请尝试调用CommandManager上的InvalidateRequerySuggested方法来强制重新查询CanExecute 方法。

祝你好运!

Try setting a breakpoint inside of DeleteNoteCommandCanExecute and checking:

  1. if it is being called before opening the context menu and
  2. the code inside DeleteNoteCommandCanExecute doesn't throw an exception (e.g. note parameter being null)

In the first case, if it is not being called, try calling the InvalidateRequerySuggested method on the CommandManager to force a requery of the CanExecute method.

Good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文