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:
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();
}