【问题标题】:RoutedCommand in ContextMenu in UserControlUserControl 中 ContextMenu 中的 RoutedCommand
【发布时间】:2013-07-05 16:56:26
【问题描述】:

情况:

我有一个静态的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>

然后在我的ContextMenu 我的MyUserControl 中这样使用它:

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

问题:

ContextMenu 出现,但 GrowingOperationExecutedGrowingOperationCanExecute 都没有被调用。打开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();
    }

【问题讨论】:

    标签: c# xaml user-controls routed-commands


    【解决方案1】:

    我会将您的 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"/>
    

    我唯一的问题是:你们是如何实现GrowingOperationExecutedGrowingOperationCanExecute 的?

    【讨论】:

    • 我在MyUserControl 后面的代码中实现了这两种方法,然后在MainWindow 中使用了这个用户控件。尝试您的解决方案...
    • 如果它不起作用,您介意发布GrowingOperationCanExecute 以确保它正确实施吗?
    • 好的。改为使用您的 UICommands 并清理 XAML 代码。还是行不通。如前所述,方法中的断点甚至没有被执行,但我将在上面发布这两种方法。
    • 您在上面说过您的上下文菜单上没有悬停动画?在填充 ContextMenu 后尝试运行 CommandManager.InvalidateQuerySuggested();。另外,我注意到我可能犯了一个错误。在 RoutedCommand 定义中,而不是 typeof(WINDOWNAME) 尝试 typeof(Commands)
    • 已使用typeof(Commands)CommandManager.InvalidateRequerySuggested() 像上面的编辑 2?没用……
    【解决方案2】:

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

    【讨论】:

    • 不鼓励这样的答案。不要发布指向包含答案的博客文章的链接。您的答案应该是某人必须做的最后一项研究。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-30
    相关资源
    最近更新 更多