绑定命令时WPF MenuItem呈灰色显示

时间:2015-11-12 22:39:48

标签: c# wpf xaml

我正在尝试将命令连接到TaskbarIcon中的上下文菜单项,但每次我这样做时,它们都会变灰。 这是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>

我的支持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();
            }
        }
    }
}

我一直在玩它并环顾四周,但我似乎无法让它发挥作用。有没有人有可能的解决方案?

1 个答案:

答案 0 :(得分:1)

来自this article

  

ContextMenus是具有自己的VisualTree和LogicalTree的独立窗口。   [...] CommandManager在当前焦点范围内搜索CommandBindings。如果当前焦点范围没有命令绑定,则将焦点范围传输到父焦点范围。启动应用程序时,未设置焦点范围。您可以通过致电FocusManager.GetFocusedElement(this)来查看此信息,您将收到空。

     

最简单的解决方案是初步设置逻辑焦点:

public Window1()
{
    InitializeComponent();

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

另一种解决方案是将CommandTarget手动绑定到父ContextMenu。

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