C# 绑定命令时,WPF菜单项灰显

C# 绑定命令时,WPF菜单项灰显,c#,wpf,xaml,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.co

我试图将命令连接到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>

我的支持是:

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();
            }
        }
    }
}
namespace Stickie.StickieNotes.WPFGUI
{
/// 
///App.xaml的交互逻辑
/// 
公共部分类应用程序:应用程序
{
静态应用程序()
{
初始化命令();
}
静态void初始化命令()
{
类型ownerType=typeof(应用程序);
OpenSettingsCommand=新路由命令(“OpenSettings”,所有者类型);
ExitApplicationCommand=新路由命令(“ExitApplication”,所有者类型);
CommandBinding openSettings=新建CommandBinding(OpenSettingsCommand、OpenSettingsExecuted、OpenSettingCanExecute);
CommandBinding exitApplication=新CommandBinding(ExitApplicationCommand、ExitApplicationExecuted、ExitApplicationCanExecute);
RegisterClassCommandBinding(ownerType,openSettings);
RegisterClassCommandBinding(所有者类型,exitApplication);
}
公共静态路由命令OpenSettingsCommand;
公共静态路由命令ExitApplicationCommand;
私有静态void ExitApplicationCanExecute(对象发送方,CanExecuteRoutedEventArgs e)
{
e、 CanExecute=true;
}
私有静态void OpenSettingCanExecute(对象发送方,CanExecuteRouteEventArgs e)
{
e、 CanExecute=true;
}
私有静态无效ExitApplicationExecuted(对象发送方,ExecutedRoutedEventArgs e)
{
应用。当前。关机(0);
}
私有静态void opensetings已执行(对象发送方,已执行路由EventTargets e)
{
if(Application.Current.MainWindow!=null)
{
Application.Current.MainWindow.Show();
}
}
}
}
我一直在玩弄它,环顾四周,但我似乎无法让它发挥作用。有人有可能的解决方案吗?

来自:

上下文菜单是单独的窗口,具有自己的VisualTree和LogicalTree。 […]CommandManager在当前焦点范围内搜索CommandBinding。如果当前焦点作用域没有命令绑定,则会将焦点作用域转移到父焦点作用域。启动应用程序时,不会设置焦点范围。您可以通过调用
FocusManager.GetFocusedElement(this)
来检查这一点,您将收到null

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

另一种解决方案是手动将CommandTarget绑定到父上下文菜单



在我在原始帖子中发布的xaml中,您将看到我已经将CommandTarget绑定到父上下文菜单。即使你所展示的装订方式似乎也没有什么不同。你说得对,对不起,我没有注意到这一点。奇怪,它不起作用,那么,它对我起作用了。您在输出窗口中看到任何绑定错误吗?没有,没有一个错误。我已经将TaskbarIcon从一个资源字典移到了一个窗口,但仍然没有骰子,所以目前我使用的事件是有效的,但我想让命令起作用。我唯一的另一个想法是,它可能与您正在使用的第三方组件有关。当您在常规WPF窗口上使用上下文菜单时,您可以尝试这样做。
public Window1()
{
    InitializeComponent();

    // Set the logical focus to the window
    Focus();
}
<MenuItem Header="Cut" Command="Cut" CommandTarget="
          {Binding Path=PlacementTarget, 
          RelativeSource={RelativeSource FindAncestor, 
          AncestorType={x:Type ContextMenu}}}"/>