This is a migrated thread and some comments may be shown as answers.

Problem with keyboard navigation in RadMenu

1 Answer 94 Views
Menu
This is a migrated thread and some comments may be shown as answers.
hwsoderlund
Top achievements
Rank 1
hwsoderlund asked on 13 Nov 2009, 01:52 PM
I cannot find a way of accessing the items in a menu when using the keyboard only. If I first use the mouse to open one of the menu items I can then proceed to navigate with the keyboard. It is just that initial step when moving focus from RadMenu and into the menu items that is missing. I have tried using the space bar, the enter key, the arrow keys, the page down keys etc., but nothing seems to work.

I did come up with a workaround, a behavior that attaches to RadMenu and listens to the enter key and the down-arrow key. When either of these keys are pressed I invoke the automation peer of the first available menu item. It is a little crude, but seems to work ok. I'm attaching the code below.

/Henrik


using System; 
using System.Net; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Documents; 
using System.Windows.Ink; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Animation; 
using System.Windows.Shapes; 
using System.Windows.Interactivity; 
using System.Windows.Data; 
using Telerik.Windows.Controls; 
using System.Windows.Automation.Peers; 
using System.Windows.Automation.Provider; 
 
namespace GLS.Gui.Helper.Behaviors 
    /// <summary> 
    /// When attached to a RadMenu, the menu will respond to the Enter and Down keys.  
    /// If one of the keys are pressed when the menu is in focus, the first item in the menu will expand. 
    /// </summary> 
    public class ExpandFirstItemOnKeyboardNav : Behavior<RadMenu> 
    { 
        /// <summary> 
        /// Called after the behavior is attached to an AssociatedObject. 
        /// </summary> 
        /// <remarks>Override this to hook up functionality to the AssociatedObject.</remarks> 
        protected override void OnAttached() 
        { 
            this.AssociatedObject.KeyDown += AssociatedObject_KeyDown; 
 
            base.OnAttached(); 
        } 
 
        void AssociatedObject_KeyDown(object sender, KeyEventArgs e) 
        { 
            if ((e.Key == Key.Down || e.Key == Key.Enter) && AssociatedObject.Items.Count > 0) 
            { 
                var item = (AssociatedObject.Items[0] as RadMenuItem); 
 
                // Create an automation peer 
                var peer = new RadMenuItemAutomationPeer(item); 
 
                // Create an InvokeProvider 
                var invokeProvider = peer.GetPattern(PatternInterface.ExpandCollapse) as IInvokeProvider; 
 
                // Invoke 
                invokeProvider.Invoke(); 
            } 
        } 
 
        /// <summary> 
        /// Called when the behavior is being detached from its AssociatedObject, but before it has actually occurred. 
        /// </summary> 
        /// <remarks>Override this to unhook functionality from the AssociatedObject.</remarks> 
        protected override void OnDetaching() 
        { 
            this.AssociatedObject.KeyDown -= AssociatedObject_KeyDown; 
 
            base.OnDetaching(); 
        } 
    } 
 

1 Answer, 1 is accepted

Sort by
0
Hristo
Telerik team
answered on 16 Nov 2009, 03:53 PM
Hello Henrik,

The problem here is that RadMenuItem.IsSubmenuOpen is read only property. If we make it read/write then you will be able to open it with code. The problem is that it is not easy to handle the case when it is set in xaml (TransformToVisual will throw exception if elements are not in the visual tree yet), also popups should be opened in the correct order (first root menu item popup then the inner one).
There were no requests for this feature (until now). we will investigate if public setter will be added for the next Q.
As a side note your code seems to do the trick.

Greetings,
Hristo
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
Menu
Asked by
hwsoderlund
Top achievements
Rank 1
Answers by
Hristo
Telerik team
Share this question
or