Browse Source

Focus first item when menu opened by keyboard.

pull/58/head
Steven Kirk 11 years ago
parent
commit
e5feeb3fc1
  1. 22
      Perspex.Controls/MenuItem.cs
  2. 2
      Perspex.Input/FocusManager.cs

22
Perspex.Controls/MenuItem.cs

@ -216,15 +216,13 @@ namespace Perspex.Controls
/// <param name="e">The event args.</param> /// <param name="e">The event args.</param>
protected override void OnKeyDown(KeyEventArgs e) protected override void OnKeyDown(KeyEventArgs e)
{ {
base.OnKeyDown(e);
switch (e.Key) switch (e.Key)
{ {
case Key.Down: case Key.Down:
if (this.IsTopLevel && this.HasSubMenu) if (this.IsTopLevel && this.HasSubMenu)
{ {
this.IsSubMenuOpen = true;
this.SelectedIndex = 0; this.SelectedIndex = 0;
this.IsSubMenuOpen = true;
e.Handled = true; e.Handled = true;
} }
@ -239,6 +237,8 @@ namespace Perspex.Controls
break; break;
} }
base.OnKeyDown(e);
} }
/// <summary> /// <summary>
@ -328,6 +328,7 @@ namespace Perspex.Controls
if (popup != null) if (popup != null)
{ {
popup.PopupRootCreated += this.PopupRootCreated; popup.PopupRootCreated += this.PopupRootCreated;
popup.Opened += this.PopupRootOpened;
} }
} }
@ -398,5 +399,20 @@ namespace Perspex.Controls
this.Presenter = presenter; 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();
}
}
}
} }
} }

2
Perspex.Input/FocusManager.cs

@ -61,7 +61,7 @@ namespace Perspex.Input
{ {
var scope = control.GetSelfAndVisualAncestors() var scope = control.GetSelfAndVisualAncestors()
.OfType<IFocusScope>() .OfType<IFocusScope>()
.FirstOrDefault(); .FirstOrDefault() ?? this.Scope;
if (scope != null) if (scope != null)
{ {

Loading…
Cancel
Save