1

Situation:

I have a static RoutedCommand defined like this:

public static class Commands
{
    public static readonly RoutedCommand GrowingOperation = new RoutedCommand("GrowingOperation", typeof(GrowingDisplay));
}

In my MyUserControl.xaml I define the command like this:

<UserControl.CommandBindings>
    <CommandBinding Command="{x:Static local:Commands.GrowingOperation}"
                    Executed="GrowingOperationExecuted"
                    CanExecute="GrowingOperationCanExecute"/>
</UserControl.CommandBindings>

And then use it like this in my ContextMenu of my MyUserControl:

<UserControl.ContextMenu>
    <ContextMenu x:Name="GrowingContextMenu">
        <MenuItem Header="Grow"
                      Command="{x:Static local:Commands.GrowingOperation}"
                      CommandParameter="grow"/>
    </ContextMenu>
</UserControl.ContextMenu>

Issue:

The ContextMenu appears, but neither the GrowingOperationExecuted nor the GrowingOperationCanExecute get called. Neither do I get any exception when opening the ContextMenu.

The open ContextMenu looks like this: enter image description here

It seems to be enabled, but there is absolute no interaction, not even a hover animation. Where is the error here?

EDIT:

Here the implementation of the command methods:

    private void GrowingOperationExecuted(object sender, ExecutedRoutedEventArgs e)
    {
        if (e.Parameter == null)
            throw new ArgumentException("ExecutedRoutedEventArgs must contain parameter.");
        var task = e.Parameter.ToString().ToLower();
        switch (task)
        {
            case "grow":
                Growing.SpeedUpGrowing();
                break;
            default:
                throw  new ArgumentOutOfRangeException();
        }
    }

    private void GrowingOperationCanExecute(object sender, CanExecuteRoutedEventArgs e)
    {
        if (e.Parameter == null)
            throw new ArgumentException("ExecutedRoutedEventArgs must contain parameter.");
        var task = e.Parameter.ToString().ToLower();
        switch (task)
        {
            case "grow":
                e.CanExecute = Growing.CanSpeedUpGrowing();
                break;
            default:
                throw new ArgumentOutOfRangeException();
        }
    }

EDIT 2:

The constructor of my MyUserControl:

public GrowingDisplay()
    {
        InitializeComponent();

        HeightProperty.AddOwner(typeof (GrowingDisplay),
                                new FrameworkPropertyMetadata(OnHeightPropertyChanged));
        WidthProperty.AddOwner(typeof (GrowingDisplay),
                               new FrameworkPropertyMetadata(OnWidthPropertyChanged));

        CommandManager.InvalidateRequerySuggested();
    }
Stefan Over
  • 5,851
  • 2
  • 35
  • 61

2 Answers2

0

I would change the definition of your RoutedCommand to:

private static RoutedUICommand _GrowingOperation;
public static RoutedCommand GrowingOperation
{
    get
    {
        if(_GrowingOperation == null)
        {
            _GrowingOperation = new RoutedUICommand("GrowingOperation", 
                                "GrowingOperation", typeof(WINDOWNAME));
        }
        return _GrowingOperation;
}

Then you can clean up your XAML by bringing the Commands class in with:

xmlns:commands="clr-namespace:NAMESPACE.Commands"

Put this in the opening Window tag. (Assuming this is a Window) Then when you set your command you can use:

<UserControl.CommandBindings>
<CommandBinding Command="commands:Commands.GrowingOperation"
                Executed="GrowingOperationExecuted"
                CanExecute="GrowingOperationCanExecute"/>

My only question is this: How are you implementing GrowingOperationExecuted and GrowingOperationCanExecute?

Th3BFG
  • 305
  • 1
  • 12
  • I implement the both methods in the code behind of my `MyUserControl` and then use this usercontrol in my `MainWindow`. Trying your solution... – Stefan Over Jul 05 '13 at 17:30
  • If it doesn't work, would you mind posting `GrowingOperationCanExecute` just to make sure it is implemented correctly? – Th3BFG Jul 05 '13 at 17:33
  • Ok. Used your UICommands instead and cleaned up XAML code. Still not working. Like said before, the breakpoint in the method doesn't even get executed, but I'll post both methods above. – Stefan Over Jul 05 '13 at 17:37
  • You said above that you don't get a hover animation over your context menu? Try running `CommandManager.InvalidateQuerySuggested();` after you populate the ContextMenu. Also, I noticed that I may have made an error. In the RoutedCommand definition, instead of `typeof(WINDOWNAME)` try `typeof(Commands)` – Th3BFG Jul 05 '13 at 18:13
  • Used `typeof(Commands)` already. `CommandManager.InvalidateRequerySuggested()` like above in edit 2? Doesn't help... – Stefan Over Jul 05 '13 at 18:19
  • FYI: If I remove the command from the `MenuItem` in the `ContextMenu`, the hover effect works again. – Stefan Over Jul 06 '13 at 10:06
0

This post should be helpful for solving your problem: http://wpftutorial.net/RoutedCommandsInContextMenu.html.

B.S.
  • 1
  • Such answers are discouraged. Dont post links to blog posts which contain the answer. YOUR answer should be the last point of research someone has to do. – Manuel Aug 17 '13 at 15:56