From e5feeb3fc140f4b629402de0763e4fe8f015591c Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Wed, 10 Jun 2015 00:25:36 +0200 Subject: [PATCH] Focus first item when menu opened by keyboard. --- Perspex.Controls/MenuItem.cs | 22 +++++++++++++++++++--- Perspex.Input/FocusManager.cs | 2 +- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/Perspex.Controls/MenuItem.cs b/Perspex.Controls/MenuItem.cs index 84aba0f4b5..31cbe4bfe7 100644 --- a/Perspex.Controls/MenuItem.cs +++ b/Perspex.Controls/MenuItem.cs @@ -216,15 +216,13 @@ namespace Perspex.Controls /// The event args. protected override void OnKeyDown(KeyEventArgs e) { - base.OnKeyDown(e); - switch (e.Key) { case Key.Down: if (this.IsTopLevel && this.HasSubMenu) { - this.IsSubMenuOpen = true; this.SelectedIndex = 0; + this.IsSubMenuOpen = true; e.Handled = true; } @@ -239,6 +237,8 @@ namespace Perspex.Controls break; } + + base.OnKeyDown(e); } /// @@ -328,6 +328,7 @@ namespace Perspex.Controls if (popup != null) { popup.PopupRootCreated += this.PopupRootCreated; + popup.Opened += this.PopupRootOpened; } } @@ -398,5 +399,20 @@ namespace Perspex.Controls this.Presenter = presenter; } } + + private void PopupRootOpened(object sender, EventArgs e) + { + var selected = this.SelectedItem; + + if (selected != null) + { + var container = this.ItemContainerGenerator.GetContainerForItem(selected); + + if (container != null) + { + container.Focus(); + } + } + } } } diff --git a/Perspex.Input/FocusManager.cs b/Perspex.Input/FocusManager.cs index 35703a71ee..9f2a6db517 100644 --- a/Perspex.Input/FocusManager.cs +++ b/Perspex.Input/FocusManager.cs @@ -61,7 +61,7 @@ namespace Perspex.Input { var scope = control.GetSelfAndVisualAncestors() .OfType() - .FirstOrDefault(); + .FirstOrDefault() ?? this.Scope; if (scope != null) {