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

后端 未结 4 954
无人及你
无人及你 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:07

    Completely different track, now: there is indeed something special about the ContextMenu as the carrier for commands: the menu is not regarded as part of the window and therefore does not behave like an element in its visual tree would.

    There are different solutions for your problems defined here: http://www.wpftutorial.net/RoutedCommandsInContextMenu.html

    The easiest approach seems to be adding this to your XAML (for the window):

    FocusManager.FocusedElement="{Binding RelativeSource={x:Static RelativeSource.Self}, Mode=OneTime}"
    

提交回复
热议问题