locked
How to avoid disabled Context menu items? RRS feed

  • Question

  • I have several controls as children of Canvas which in turn sits inside my 'main' UserControl. Each of these child controls have a context menu. If I click anywhere outside this main User Control and then right mouse button click over any child control, all context menu items are displayed as disabled.

     

    What I have to do is first click left mouse button anywhere inside the main UserControl. Only then will right button click show context menu items as enabled.

     

    Investigation indicates that context menus are disabled when none of the controls have keyboard focus. Left button click apparently automagically finds some focusable control and sets focus on it. After that context menu items display as enabled.

     

    As this is not a friendly UI (user has to left click before right click will make context menus functional) I am looking for a generic way to avoid it. For example, on right button clcik I may try to discover any focusable child control and explicitly set focus on it (all controls are FrameworkElements so there is Focus() method available)

     

    Is this the best solution and, if yes, what is the best way to discover focusable control inside some parent control? For ex. should I navigate logical or visual tree or both?

    Friday, April 27, 2007 11:39 PM

Answers

  • Most GUI frameworks don't change focus on right-click (which is wrong for most cases, in my opinion, but that's the fact).

    WPF event routing can depend on focus. This is sometimes unexpected. Setting menu item status involves routing the query commands. To avoid the dependence on focus, set the CommandTarget property of each menu item (using a Binding).

    Wednesday, May 2, 2007 12:59 AM

All replies

  • Don't have time try this right this second, but I think you might just need to set IsFocusable="true" on the root element of your UserControl template.

     

    -Drew

    Saturday, April 28, 2007 12:17 AM
  • Hmm.. all that did is add focus rectangle around Canvas (child of UserControl that contains other controls) but right mouse click context menu invocation still shows disabled items.
    Monday, April 30, 2007 10:59 PM
  • Most GUI frameworks don't change focus on right-click (which is wrong for most cases, in my opinion, but that's the fact).

    WPF event routing can depend on focus. This is sometimes unexpected. Setting menu item status involves routing the query commands. To avoid the dependence on focus, set the CommandTarget property of each menu item (using a Binding).

    Wednesday, May 2, 2007 12:59 AM