移动到下一个控件上输入WPF键preSS [英] Moving to next control on Enter keypress in WPF

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

问题描述

我想移动到下一个控件,当我preSS Enter键,而不是在WPF MVVM应用程序中的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?

推荐答案

周杰伦的答案是好的,如果你只是想让它几文本框工作,但如果你希望你的整个应用程序的工作,这样我绝对赞成makwana.a的回答。这里是我的makwana.a的回答,我在C#中使用的修改。我还包括对移动到下一个控件,如果当前控制是复选框通过输入支持。

The answer by Jay is nice if you only want it to work for a few text boxes, but if you want your whole application to work that way I definitely favor makwana.a's answer. Here is my modification of makwana.a's answer which I use in C#. I also included support for moving on to the next control via enter if the active control is a check box.

另外请注意,而不是使用标签属性来表示焦点是否应该继续前进,我使用文本框的AcceptsReturn财产,因为它默认为false,您只需将其设置为true的文本框您要的多线使用,即不应该将焦点移到上输入密钥。

Also note that Instead of using the tag property to denote whether or not the focus should move on, I use the AcceptsReturn property of the text box since it defaults to false and you'll only set it to true on the text boxes that you want to use as multi-line i.e. that should not move focus on Enter Key.

声明这些事件处理程序

        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;
        }
    }

修改

我更新了code,以纪念击键处理,如果运动成功,也切换复选框,因为键被处理,将不再实现它。

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键preSS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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