WPF 回车转Tab实现跳转
1.重写窗体的KeyDown事件
protected override void OnKeyDown(KeyEventArgs e) { if (e.Key == Key.Enter) { // MoveFocus takes a TraveralReqest as its argument. TraversalRequest request = new TraversalRequest(FocusNavigationDirection.Next); // Gets the element with keyboard focus. UIElement elementWithFocus = Keyboard.FocusedElement as UIElement; // Change keyboard focus. if (elementWithFocus != null) { elementWithFocus.MoveFocus(request); } e.Handled = true; } base.OnKeyDown(e); }
2.在基容器如Grid的KeyDown事件中
private void Grid_KeyDown(object sender, KeyEventArgs e) { var uie = e.OriginalSource as UIElement; if (e.Key == Key.Enter) { uie.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next)); e.Handled = true; } }