1

情况:

我有一个RoutedCommand这样定义的静态:

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

在我的MyUserControl.xaml我定义这样的命令:

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

然后在 my ContextMenuof my中像这样使用它MyUserControl

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

问题:

出现ContextMenu,但既GrowingOperationExecuted没有被调用,也没有GrowingOperationCanExecute被调用。打开ContextMenu.

打开ContextMenu看起来像这样: 在此处输入图像描述

它似乎已启用,但绝对没有交互,甚至没有悬停动画。这里的错误在哪里?

编辑:

这里执行命令方法:

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

编辑2:

我的构造函数MyUserControl

public GrowingDisplay()
    {
        InitializeComponent();

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

        CommandManager.InvalidateRequerySuggested();
    }
4

2 回答 2

0

这篇文章应该有助于解决您的问题: http ://wpftutorial.net/RoutedCommandsInContextMenu.html 。

于 2013-08-17T15:38:30.293 回答
0

我会将您的 RoutedCommand 的定义更改为:

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

然后你可以通过引入 Commands 类来清理你的 XAML:

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

把它放在打开的 Window 标记中。(假设这是一个窗口)然后,当您设置命令时,您可以使用:

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

我唯一的问题是:你是如何实施GrowingOperationExecuted和的GrowingOperationCanExecute

于 2013-07-05T17:16:43.530 回答