簡體   English   中英

綁定命令時WPF MenuItem變灰

[英]WPF MenuItem grayed out when binding Command

我試圖將命令連接到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();
            }
        }
    }
}

我一直在玩它,環顧四周,但似乎無法正常工作。 有沒有人有可能的解決方案?

這篇文章

ContextMenus是具有各自的VisualTree和LogicalTree的單獨窗口。 [...] CommandManager在當前焦點范圍內搜索CommandBindings。 如果當前焦點范圍沒有命令綁定,則它將焦點范圍轉移到父焦點范圍。 啟動應用程序時,未設置焦點范圍。 您可以通過調用FocusManager.GetFocusedElement(this)進行檢查,您將收到null。

最簡單的解決方案是首先設置邏輯焦點:

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}}}"/>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM