private void HandleKeyPress(object sender, KeyEventArgs e) { switch (e.Key) { case Key.Tab: if (SelectedItem != null) { CommitSelectionAndMoveFocus(); } break; case Key.Enter: CommitSelectionAndMoveFocus(); e.Handled = true; break; case Key.Escape: Flyout.IsOpen = false; TargetLocationSearchTextBox.ScrollToEnd(); e.Handled = true; break; case Key.Down: if (Options.Items.Count > 0) { Options.ScrollIntoView(Options.Items[0]); FrameworkElement fe = (FrameworkElement)Options.ItemContainerGenerator.ContainerFromIndex(0); fe?.Focus(); Options.SelectedIndex = 0; e.Handled = true; } break; } }
private void OnLostFocus(object sender, RoutedEventArgs e) { if (SelectedItem != null && !Options.IsKeyboardFocusWithin) { Commit(SelectedItem); TargetLocationSearchTextBox.ScrollToEnd(); } }
private void HandleListBoxKeyPress(object sender, KeyEventArgs e) { int index = TargetLocationSearchTextBox.CaretIndex; switch (e.Key) { case Key.Tab: case Key.Enter: CommitSelectionAndMoveFocus(); e.Handled = true; break; case Key.Up: if (Options.SelectedIndex == 0) { SelectedItem = Items[0]; LostFocus -= OnLostFocus; TargetLocationSearchTextBox.Focus(); TargetLocationSearchTextBox.CaretIndex = index; LostFocus += OnLostFocus; } break; case Key.Escape: Flyout.IsOpen = false; TargetLocationSearchTextBox.ScrollToEnd(); e.Handled = true; break; case Key.Down: case Key.PageDown: case Key.PageUp: case Key.Home: case Key.End: break; default: LostFocus -= OnLostFocus; TargetLocationSearchTextBox.Focus(); TargetLocationSearchTextBox.CaretIndex = index; LostFocus += OnLostFocus; break; } }
private void HandleKeyPress(object sender, KeyEventArgs e) { switch (e.Key) { case Key.Tab: // SelectedItem could be null if the key press came from keyboard navigation and not commit operation. // In this case we will just move the focus to next control. if (SelectedItem == null) { TargetLocationSearchTextBox.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next)); } else { CommitSelectionAndMoveFocus(); } e.Handled = true; break; case Key.Enter: CommitSelectionAndMoveFocus(); e.Handled = true; break; case Key.Escape: Flyout.IsOpen = false; TargetLocationSearchTextBox.ScrollToEnd(); e.Handled = true; break; case Key.Down: if (Options.Items.Count > 0) { Options.ScrollIntoView(Options.Items[0]); FrameworkElement fe = (FrameworkElement)Options.ItemContainerGenerator.ContainerFromIndex(0); fe?.Focus(); Options.SelectedIndex = 0; e.Handled = true; } break; } }