TextBoxEnterAsTabBehavior.cs 761 B

1234567891011121314151617181920212223242526272829
  1. using System.Windows.Controls;
  2. using System.Windows.Input;
  3. using Microsoft.Xaml.Behaviors;
  4. namespace InABox.WPF;
  5. public class TextBoxEnterAsTabBehavior :
  6. Behavior<TextBox>
  7. {
  8. protected override void OnAttached()
  9. {
  10. base.OnAttached();
  11. AssociatedObject.PreviewKeyDown += AssociatedObjectOnPreviewKeyDown;
  12. }
  13. protected override void OnDetaching()
  14. {
  15. AssociatedObject.PreviewKeyDown -= AssociatedObjectOnPreviewKeyDown;
  16. base.OnDetaching();
  17. }
  18. private void AssociatedObjectOnPreviewKeyDown(object sender, KeyEventArgs args)
  19. {
  20. if (args.Key != Key.Enter) { return; }
  21. args.Handled = true;
  22. AssociatedObject.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
  23. }
  24. }