private void ChangeFocusedItem(bool ascendIndex) { Type focusedElementType = FocusManager.GetFocusedElement().GetType(); MenuItem item; int startIndex; if (focusedElementType == _itemContainerList.GetType() && _items.Count > 0) { // focused item is the item container list, so try to set focus to an initial item startIndex = GetNextItemIndex(-1, ascendIndex); } else if (focusedElementType != typeof(MenuItem)) { // focused item isn't the item container list or a menu item, so ignore return; } else { // focused item is already a menu item, so try to set focus to next item = FocusManager.GetFocusedElement() as MenuItem; startIndex = GetNextItemIndex(_items.IndexOf(item), ascendIndex); } int index = startIndex; MenuItemBase nextItem = _items[index]; // focus next item, if it's a menu item if (nextItem.GetType() == typeof(MenuItem)) { nextItem.Focus(Windows.UI.Xaml.FocusState.Programmatic); } else { // next element wasn't a MenuItem, so loop through once trying to find the next item index = GetNextItemIndex(index, ascendIndex); nextItem = _items[index]; while (nextItem.GetType() != typeof(MenuItem) && index != startIndex) { index = GetNextItemIndex(index, ascendIndex); nextItem = _items[index]; } if (nextItem.GetType() == typeof(MenuItem)) { nextItem.Focus(Windows.UI.Xaml.FocusState.Programmatic); } } }
private void ChangeFocusedItem(bool ascendIndex) { var focusedElement = FocusManager.GetFocusedElement(); int startIndex; if (focusedElement != null && ((focusedElement == _itemContainerList || focusedElement == this) && _items.Count > 0)) { // focused item is the menu or the item container list, so try to set focus to an initial item startIndex = GetNextItemIndex(-1, ascendIndex); } else if (focusedElement != null && (focusedElement is MenuItem && _items.Contains((MenuItem)focusedElement))) { // focused item is already one of our menu items, so try to set focus to next startIndex = GetNextItemIndex(_items.IndexOf((MenuItem)focusedElement), ascendIndex); } else { // focus is outside of the menu return; } int index = startIndex; MenuItemBase nextItem = _items[index]; // focus next item, if it's a menu item if (nextItem is MenuItem) { nextItem.Focus(Windows.UI.Xaml.FocusState.Programmatic); } else { // next element wasn't a MenuItem, so loop through once trying to find the next item index = GetNextItemIndex(index, ascendIndex); nextItem = _items[index]; while (nextItem != null && (!(nextItem is MenuItem) && index != startIndex)) { index = GetNextItemIndex(index, ascendIndex); nextItem = _items[index]; } if (nextItem != null && nextItem is MenuItem) { nextItem.Focus(Windows.UI.Xaml.FocusState.Programmatic); } } }