简体   繁体   English

绑定命令时WPF MenuItem变灰

[英]WPF MenuItem grayed out when binding Command

I'm trying to hook up commands to context menu items in a TaskbarIcon but everytime I do, they become grayed out. 我试图将命令连接到TaskbarIcon中的上下文菜单项,但是每次执行时,它们都会变灰。 Here's the XAML: 这是XAML:

<ResourceDictionary
                xmlns:local="clr-namespace:Stickie.StickieNotes.WPFGUI"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:tb="http://www.hardcodet.net/taskbar"
>
<!-- Globally declared notify icon -->
<tb:TaskbarIcon x:Key="MyNotifyIcon">
    <tb:TaskbarIcon.ContextMenu>
        <ContextMenu>
            <MenuItem Header="Open Settings" Command="local:App.OpenSettingsCommand" 
                      CommandTarget="{Binding Path=PlacementTarget, RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}"/>
            <MenuItem Header="New Note"/>
            <MenuItem Header="Exit" Command="local:App.ExitApplicationCommand"
                      CommandTarget="{Binding Path=PlacementTarget, RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}"/>
        </ContextMenu>
    </tb:TaskbarIcon.ContextMenu>
</tb:TaskbarIcon>

And my backing CS: 和我的支持CS:

namespace Stickie.StickieNotes.WPFGUI
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        static App()
        {
            initializeCommands();
        }
        static void initializeCommands()
        {
            Type ownerType = typeof (App);
            OpenSettingsCommand = new RoutedCommand("OpenSettings", ownerType);
            ExitApplicationCommand = new RoutedCommand("ExitApplication", ownerType);
            CommandBinding openSettings = new CommandBinding(OpenSettingsCommand, OpenSettingsExecuted, OpenSettingCanExecute);
            CommandBinding exitApplication = new CommandBinding(ExitApplicationCommand, ExitApplicationExecuted, ExitApplicationCanExecute);
            CommandManager.RegisterClassCommandBinding(ownerType,openSettings);
            CommandManager.RegisterClassCommandBinding(ownerType,exitApplication);
        }
        public static RoutedCommand OpenSettingsCommand;
        public static RoutedCommand ExitApplicationCommand;

        private static void ExitApplicationCanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }

        private static void OpenSettingCanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }

        private static void ExitApplicationExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            Application.Current.Shutdown(0);
        }

        private static void OpenSettingsExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            if (Application.Current.MainWindow != null)
            {
                Application.Current.MainWindow.Show();
            }
        }
    }
}

I've been playing around with it and looking around a ton but I can't seem to get it to work. 我一直在玩它,环顾四周,但似乎无法正常工作。 Does anyone have a possible solution? 有没有人有可能的解决方案?

From this article : 这篇文章

ContextMenus are separate windows with their own VisualTree and LogicalTree. ContextMenus是具有各自的VisualTree和LogicalTree的单独窗口。 [...] the CommandManager searches for CommandBindings within the current focus scope. [...] CommandManager在当前焦点范围内搜索CommandBindings。 If the current focus scope has no command binding, it transfers the focus scope to the parent focus scope. 如果当前焦点范围没有命令绑定,则它将焦点范围转移到父焦点范围。 When you startup your application the focus scope is not set. 启动应用程序时,未设置焦点范围。 You can check this by calling FocusManager.GetFocusedElement(this) and you will receive null. 您可以通过调用FocusManager.GetFocusedElement(this)进行检查,您将收到null。

The simplest solution is to initially set the logical focus: 最简单的解决方案是首先设置逻辑焦点:

public Window1()
{
    InitializeComponent();

    // Set the logical focus to the window
    Focus();
}

Another solution is to manually bind the CommandTarget to the parent ContextMenu. 另一个解决方案是将CommandTarget手动绑定到父ContextMenu。

<MenuItem Header="Cut" Command="Cut" CommandTarget="
          {Binding Path=PlacementTarget, 
          RelativeSource={RelativeSource FindAncestor, 
          AncestorType={x:Type ContextMenu}}}"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM