diff --git a/Perspex.Controls/MenuItem.cs b/Perspex.Controls/MenuItem.cs index 307cfbc607..12572ae751 100644 --- a/Perspex.Controls/MenuItem.cs +++ b/Perspex.Controls/MenuItem.cs @@ -75,6 +75,11 @@ namespace Perspex.Controls /// private IDisposable submenuTimer; + /// + /// The submenu popup. + /// + private Popup popup; + /// /// Initializes static members of the class. /// @@ -268,6 +273,10 @@ namespace Perspex.Controls } break; + + default: + e.Handled = this.HandleAccessKey(e.Text); + break; } if (!passStraightToParent) @@ -276,6 +285,52 @@ namespace Perspex.Controls } } + private bool HandleAccessKey(string text) + { + text = text.ToUpper(); + + if (!this.IsSubMenuOpen) + { + var match = this.GetVisualDescendents() + .OfType() + .FirstOrDefault(x => x.AccessKey.ToString().ToUpper() == text); + + if (match != null) + { + this.IsSubMenuOpen = true; + return true; + } + } + else + { + var match = this.popup.PopupRoot.GetVisualDescendents() + .OfType() + .FirstOrDefault(x => x.AccessKey.ToString().ToUpper() == text) + ?.GetVisualAncestors() + .OfType() + .FirstOrDefault(); + + if (match != null) + { + var item = (MenuItem)this.ItemContainerGenerator.GetItemForContainer(match); + + if (item.HasSubMenu) + { + item.SelectedIndex = 0; + item.IsSubMenuOpen = true; + } + else + { + item.RaiseEvent(new RoutedEventArgs(ClickEvent)); + } + + return true; + } + } + + return false; + } + /// /// Called when the pointer enters the . /// @@ -358,14 +413,10 @@ namespace Perspex.Controls { base.OnTemplateApplied(); - var popup = this.FindTemplateChild("popup"); - - if (popup != null) - { - popup.PopupRootCreated += this.PopupRootCreated; - popup.Opened += this.PopupOpened; - popup.Closed += this.PopupClosed; - } + this.popup = this.GetTemplateChild("popup"); + this.popup.PopupRootCreated += this.PopupRootCreated; + this.popup.Opened += this.PopupOpened; + this.popup.Closed += this.PopupClosed; } ///