为什么此WPF RoutedCommand绑定的上下文MenuItem被禁用? [英] Why is this WPF RoutedCommand bound Context MenuItem disabled?

查看:71
本文介绍了为什么此WPF RoutedCommand绑定的上下文MenuItem被禁用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此刻,我仍在摸索WPF,无法弄清楚为什么禁用此上下文菜单项:

I'm still fumbling my way around WPF at the moment, and can not figure out why this context menu item is disabled:

<Window x:Class="DisabledMenuItemProblem.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:DisabledMenuItemProblem"
        Title="Window1" Height="300" Width="300">
    <TextBlock Text="fooooobaaaaaar">
        <TextBlock.ContextMenu>
            <ContextMenu>
                <MenuItem Header="Foo" Command="{x:Static local:MyCommands.FooBar}" />
            </ContextMenu>
        </TextBlock.ContextMenu>
    </TextBlock>
</Window>

using System.Windows;
using System.Windows.Input;

namespace DisabledMenuItemProblem
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            CommandBindings.Add(new CommandBinding(MyCommands.FooBar, FooExecuted, CanFooExecute));
        }

        public void FooExecuted(object sender, ExecutedRoutedEventArgs e)
        { MessageBox.Show("Foo!"); }

        public void CanFooExecute(object sender, CanExecuteRoutedEventArgs e)
        { e.CanExecute = true; }
    }

    public static class MyCommands
    { 
        public static RoutedCommand FooBar = new RoutedCommand(); 
    }
}

我想念什么?

让我感到困惑的是,如果我在窗口中放置一个按钮并将其命令设置为FooBar,它就会起作用,并且一旦执行它,便会启用上下文菜单!

What's also baffling me is that if i throw a button in the window and set its command to FooBar it works, and once its been executed, then the context menu gets enabled!

干杯们, 克里斯.

推荐答案

这是我使用的一般模式....

here is the general pattern that I use....

首先,将您的命令保留在自己的静态类中,这会促进重用,等等.

first, keep your commands in thier own static class, this promotes reuse,etc....

public static class MyCommands
{
    public static RoutedUICommand CmdFoo = new RoutedUICommand("CmdFoo", 
                                                               "CmdFoo", 
                                                               typeof(MyCommands));
}

第二秒,在control/window/etc中注册命令.您想在通常使用的构造函数中使用它

second, register the command in the control/window/etc. you want to use it in, normally in the constructor

public MyControl
{
    public MyControl()
    {
        CommandBindings.Add( 
            new CommandBinding( MyCommands.CmdFoo,   // this is the command object
                                XCutFooCommand,      // execute
                                CanXCuteFooCommand));// can execute?
    }

第三,在control/window/etc ...

third, create your handlers in the control/window/etc.....

  public void CanExecuteRerollCommand(object sender, CanExecuteRoutedEventArgs e)
    {
        e.CanExecute = true;  // can this command be executed?
        e.Handled = true;     // has this event been handled?
    }
    public void ExecuteRerollCommand(object sender, ExecutedRoutedEventArgs e)
    {
    // do stuff
    }
}

最后,您的xaml应该看起来像这样:

lastly, your xaml ought to look like this:

    <ContextMenu>
        <ContextMenu.CommandBindings>
            <CommandBinding Command="foo:MyCommands.CmdFoo" 
                            CanExecute="CanExecuteRerollCommand" 
                            Executed="ExecuteRerollCommand" />
        </ContextMenu.CommandBindings>
        <MenuItem Header="Reroll"  Command="foo:MyCommands.CmdFoo"/>
    </ContextMenu>

请注意,没有绑定.另外,请注意<ContextMenu>中的<CommandBinding>. 这是参考. http://www.wiredprairie.us/journal/2007/04/commandtarget_menuitem_context.html

notice that there is no binding. Also, notice the <CommandBinding> in the <ContextMenu>. here is a reference.... http://www.wiredprairie.us/journal/2007/04/commandtarget_menuitem_context.html

已被禁用的命令在此站点

这篇关于为什么此WPF RoutedCommand绑定的上下文MenuItem被禁用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆