void SearchBox_PreviewKeyDown(object sender, KeyEventArgs e) { // TODO: focus on next element if (e.Key == Key.Down && listBox.ItemCount > 0) { e.Handled = true; DirectionalNavigation.GetNext(listBox, NavigationDirection.First).Focus(); //listBox.MoveFocus(new TraversalRequest(FocusNavigationDirection.First)); listBox.SelectedIndex = 0; } }
/// <summary> /// Gets the next control in the specified navigation direction. /// </summary> /// <param name="element">The element.</param> /// <param name="direction">The navigation direction.</param> /// <returns> /// The next element in the specified direction, or null if <paramref name="element"/> /// was the last in therequested direction. /// </returns> public static IInputElement GetNext( IInputElement element, FocusNavigationDirection direction) { Contract.Requires <ArgumentNullException>(element != null); if (direction == FocusNavigationDirection.Next || direction == FocusNavigationDirection.Previous) { return(TabNavigation.GetNextInTabOrder(element, direction)); } else { return(DirectionalNavigation.GetNext(element, direction)); } }
/// <summary> /// Gets the next control in the specified navigation direction. /// </summary> /// <param name="element">The element.</param> /// <param name="direction">The navigation direction.</param> /// <returns> /// The next element in the specified direction, or null if <paramref name="element"/> /// was the last in therequested direction. /// </returns> public static IInputElement GetNext( IInputElement element, NavigationDirection direction) { Contract.Requires <ArgumentNullException>(element != null); var customHandler = element.GetSelfAndVisualAncestors() .OfType <ICustomKeyboardNavigation>() .FirstOrDefault(); if (customHandler != null) { var(handled, next) = customHandler.GetNext(element, direction); if (handled) { if (next != null) { return(next); } else if (direction == NavigationDirection.Next || direction == NavigationDirection.Previous) { return(TabNavigation.GetNextInTabOrder((IInputElement)customHandler, direction, true)); } else { return(null); } } } if (direction == NavigationDirection.Next || direction == NavigationDirection.Previous) { return(TabNavigation.GetNextInTabOrder(element, direction)); } else { return(DirectionalNavigation.GetNext(element, direction)); } }