diff --git a/Perspex.Controls/Menu.cs b/Perspex.Controls/Menu.cs index 0ea921e9e0..e4a8c6bf10 100644 --- a/Perspex.Controls/Menu.cs +++ b/Perspex.Controls/Menu.cs @@ -56,6 +56,19 @@ namespace Perspex.Controls private set { this.SetValue(IsOpenProperty, value); } } + /// + /// Gets the selected container. + /// + private MenuItem SelectedMenuItem + { + get + { + return (this.SelectedItem != null) ? + (MenuItem)this.ItemContainerGenerator.GetContainerForItem(this.SelectedItem) : + null; + } + } + /// /// Closes the menu. /// @@ -134,6 +147,8 @@ namespace Perspex.Controls /// The event args. protected override void OnKeyDown(KeyEventArgs e) { + bool menuWasOpen = this.SelectedMenuItem?.IsSubMenuOpen ?? false; + base.OnKeyDown(e); if (this.IsOpen && e.Key == Key.Escape) @@ -141,6 +156,18 @@ namespace Perspex.Controls this.CloseMenu(); e.Handled = true; } + else if (menuWasOpen) + { + // If a menu item was open and we navigate to a new one with the arrow keys, open + // that menu and select the first item. + var selection = this.SelectedMenuItem; + + if (selection != null && !selection.IsSubMenuOpen) + { + selection.IsSubMenuOpen = true; + selection.SelectedIndex = 0; + } + } } /// @@ -153,7 +180,7 @@ namespace Perspex.Controls if (menuItem != null && menuItem.Parent == this) { - foreach (var child in this.Items.OfType()) + foreach (var child in this.GetLogicalChildren().OfType()) { if (child != menuItem && child.IsSubMenuOpen) {