移动到 WPF 中 Enter 按键的下一个控件 [英] Moving to next control on Enter keypress in WPF

查看:25
本文介绍了移动到 WPF 中 Enter 按键的下一个控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 WPF MVVM 应用程序中按 Enter 键而不是 Tab 键时移动到下一个控件.我怎样才能做到这一点?

I want to move to the next control when I press the Enter key instead of the Tab key in a WPF MVVM application. How can I achieve this?

推荐答案

如果您只希望它适用于几个文本框,杰伊的回答是最好的.

If you only want it to work for a few text boxes, Jay's answer is best.

如果您希望整个应用程序都以这种方式工作,makwana.a 的答案 更好,但可以改进.

If you want your whole application to work that way, makwana.a's answer is better but can be improved.

以下是我对 makwana.a 的答案 的修改,我已在许多应用程序中使用过.如果活动控件是复选框,它还支持通过回车移动到下一个控件.我没有使用 tag 属性来决定焦点是否应该移动,而是使用了文本框的 AcceptsReturn 属性.我这样做是因为它默认为 false,并且只会在多行文本框中设置为 true.在这种情况下,您无论如何都不希望在输入时焦点移动到下一个控件.

Below is my modification of makwana.a's answer, which I have used in numerous applications. It also includes support for moving to the next control via enter if the active control is a check box. Instead of using the tag property to decide whether or not the focus should move, I used the AcceptsReturn property of the text box. I did this because it defaults to false and will only be set to true on multi-line text boxes. In that case, you won't want the focus to move to the next control on enter anyway.

在 App.xaml 的 OnStartup void 中声明这些事件处理程序

Declare these event handlers in the OnStartup void of App.xaml

        EventManager.RegisterClassHandler(typeof(TextBox), TextBox.KeyDownEvent, new KeyEventHandler(TextBox_KeyDown));
        EventManager.RegisterClassHandler(typeof(CheckBox), CheckBox.KeyDownEvent, new KeyEventHandler(CheckBox_KeyDown));

以下是使其在应用程序范围内工作所需的其余方法.

Here are the rest of the methods needed to make it work application wide.

    void TextBox_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Enter & (sender as TextBox).AcceptsReturn == false) MoveToNextUIElement(e);
    }

    void CheckBox_KeyDown(object sender, KeyEventArgs e)
    {
        MoveToNextUIElement(e);
        //Sucessfully moved on and marked key as handled.
        //Toggle check box since the key was handled and
        //the checkbox will never receive it.
        if (e.Handled == true)
        {
            CheckBox cb = (CheckBox)sender;
            cb.IsChecked = !cb.IsChecked;
        }

     }

    void MoveToNextUIElement(KeyEventArgs e)
    {
        // Creating a FocusNavigationDirection object and setting it to a
        // local field that contains the direction selected.
        FocusNavigationDirection focusDirection = FocusNavigationDirection.Next;

        // MoveFocus takes a TraveralReqest as its argument.
        TraversalRequest request = new TraversalRequest(focusDirection);

        // Gets the element with keyboard focus.
        UIElement elementWithFocus = Keyboard.FocusedElement as UIElement;

        // Change keyboard focus.
        if (elementWithFocus != null)
        {
            if (elementWithFocus.MoveFocus(request)) e.Handled = true;
        }
    }

编辑

如果移动成功,我更新了代码以将击键标记为已处理,并且由于键已被处理并且将不再到达它,因此还切换复选框.

I updated the code to mark the keystroke as handled if the movement was successful and also toggle the checkbox since the key was handled and will no longer reach it.

这篇关于移动到 WPF 中 Enter 按键的下一个控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆