internal void OnNavigationKeyDown(KeyEventArgs e)
{
int focusedIndex = ItemContainerGenerator.IndexFromContainer(e.OriginalSource as DependencyObject);
bool itemNavigateFromCurrentFocused = true;
if (focusedIndex < 0)
{
UIElement popupChild = _popup.TryGetChild();
if (popupChild != null &&
popupChild.IsKeyboardFocusWithin)
{
// If the popup already has focus within,
// then the focused element is not the item container
// itself, but is inside one such container (eg. filter button
// of gallery). Hence do not navigate in such case, but do default
// keyboard navigation.
itemNavigateFromCurrentFocused = false;
}
}
bool handled = false;
DependencyObject focusedElement = Keyboard.FocusedElement as DependencyObject;
switch (e.Key)
{
case Key.Home:
if (IsDropDownOpen)
{
handled = RibbonHelper.NavigateToNextMenuItemOrGallery(this, -1, BringIndexIntoView);
}
break;
case Key.End:
if (IsDropDownOpen)
{
handled = RibbonHelper.NavigateToPreviousMenuItemOrGallery(this, -1, BringIndexIntoView);
}
break;
case Key.Down:
if (IsDropDownOpen)
{
if (itemNavigateFromCurrentFocused)
{
// event could have bubbled up from MenuItem
// when it could not navigate to the next item (for eg. gallery)
handled = RibbonHelper.NavigateToNextMenuItemOrGallery(this, focusedIndex, BringIndexIntoView);
}
else
{
RibbonHelper.MoveFocus(FocusNavigationDirection.Down);
handled = true;
}
}
break;
case Key.Up:
if (IsDropDownOpen)
{
if (itemNavigateFromCurrentFocused)
{
// event could have bubbled up from MenuItem
// when it could not navigate to the previous item (for eg. gallery)
handled = RibbonHelper.NavigateToPreviousMenuItemOrGallery(this, focusedIndex, BringIndexIntoView);
}
else
{
RibbonHelper.MoveFocus(FocusNavigationDirection.Up);
handled = true;
}
}
break;
case Key.Tab:
if (IsDropDownOpen &&
(IsFocused || (focusedElement != null && TreeHelper.IsVisualAncestorOf(this, focusedElement))))
{
if ((Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift)
{
handled = RibbonHelper.NavigateToPreviousMenuItemOrGallery(this, Items.Count, BringIndexIntoView);
}
else
{
handled = RibbonHelper.NavigateToNextMenuItemOrGallery(this, -1, BringIndexIntoView);
}
}
break;
}
e.Handled = handled;
}