开发者

Why is my ContextMenu item bound to a RelayCommand is always disabled?

开发者 https://www.devze.com 2023-03-29 11:03 出处:网络
I\'m working on my first big WPF MVVM application now, which uses MVVM Light Toolkit in combination with Josh Smith\'s RelayCommand.

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!


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!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号