private void TextBox_KeyDown(object sender, KeyEventArgs e) { if (e.Key != Key.Back && e.Key != Key.Enter && e.Key != Key.Tab && e.Key != Key.Escape) { return; } var textBox = sender as ComboBox; if (textBox == null) { return; } if (e.Key != Key.Back) { if (string.IsNullOrWhiteSpace(textBox.Text)) { return; } AddNewTag(textBox.Text.Trim()); textBox.Text = ""; if (e.Key == Key.Tab) { FocusAdvancement.MoveFocus(textBox, Keyboard.Modifiers == ModifierKeys.Shift ? FocusNavigationDirection.Previous : FocusNavigationDirection.Next); } else if (e.Key == Key.Escape) { FocusAdvancement.RemoveFocus(textBox); } } else if (string.IsNullOrWhiteSpace(textBox.Text) || IsCaretAtFront(textBox)) { FocusAdvancement.MoveFocus(textBox, FocusNavigationDirection.Previous); } else { return; } e.Handled = true; }
protected override void OnPreviewKeyDown(KeyEventArgs e) { switch (e.Key) { case Key.Escape: e.Handled = FocusAdvancement.RemoveFocus(this); break; case Key.Enter: if (!AcceptsReturn) { e.Handled = FocusAdvancement.MoveFocus(this); } break; case Key.Tab: if (Keyboard.Modifiers == ModifierKeys.Shift && FocusAdvancement.MoveFocus(this, FocusNavigationDirection.Previous)) { e.Handled = true; } break; case Key.Up: case Key.Down: if (GetMode(this) != SpecialMode.None) { var processed = ProcessText(Text, e.Key == Key.Up ? 1d : -1d); if (processed != null) { SetTextBoxText(this, processed); } e.Handled = true; } break; } if (!e.Handled) { base.OnPreviewKeyDown(e); } }