WPF custom command in context menu are disabled until any button clicked

后端 未结 4 912
无人及你
无人及你 2020-12-31 03:48

I have a custom command and I try to execute them from the context menu, but they are always displayed as disabled unless I click any button on the UI (buttons do not have a

4条回答
  •  囚心锁ツ
    2020-12-31 04:14

    I just ran into this while trying to implement a custom context menu for AvalonDock. None of the solutions suggested above worked for me.

    I got the context menu working by explicitly registering my command handlers on the ContextMenu class in addition to the main widow. The function below is a helper I used for command registration.

        void RegisterCmd(RoutedCommand command, ExecutedRoutedEventHandler handler, CanExecuteRoutedEventHandler canExecute)
        {
            var binding = new CommandBinding(command, handler, canExecute);
            this.CommandBindings.Add(binding);
            CommandManager.RegisterClassCommandBinding(typeof(ContextMenu), binding);
        }
    

提交回复
热议问题