Interpret enter as tab WPF

后端 未结 5 1980
醉酒成梦
醉酒成梦 2021-02-01 18:52

I want to interpret Enter key as Tab key in whole my WPF application, that is, everywhere in my application when user press Enter I want to focus the next focusable control,exce

5条回答
  •  隐瞒了意图╮
    2021-02-01 19:14

    Based on Richard Aguirre's answer, which is better than the selected answer for ease of use, imho, you can make this more generic by simply changing the Grid to a UIElement.

    To change it in whole project you need to do this

    • In App.xaml.cs:

       protected override void OnStartup(StartupEventArgs e)
       {           
           EventManager.RegisterClassHandler(typeof(UIElement), UIElement.PreviewKeyDownEvent, new KeyEventHandler(Grid_PreviewKeyDown));
           base.OnStartup(e);
       }
      
       private void Grid_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
       {
           var uie = e.OriginalSource as UIElement;
      
           if (e.Key == Key.Enter)
           {
               e.Handled = true;
               uie.MoveFocus(
               new TraversalRequest(
               FocusNavigationDirection.Next));
           }
       }
      
    • Compile. And done it. Now you can use enter like tab. Note: This work for elements in the grid

提交回复
热议问题