在WPF应用程序中按Return键时,如何模拟Tab键的按下? [英] How do I simulate a Tab key press when Return is pressed in a WPF application?

查看:218
本文介绍了在WPF应用程序中按Return键时,如何模拟Tab键的按下?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WPF应用程序中,我有一个包含很多字段的窗口. 当用户在填写每个字段后使用TAB键时,Windows会了解到它会移至下一个.这是很清楚的行为.

In a WPF application, i have a window that has a lot of fields. When the user uses the TAB key after filling each field, windows understands that it moves on to the next. This is pretty know behavior.

现在我要做的是使其模拟TAB键,而实际上RETURN被命中了. 因此,在我的WPF xaml中,我暗示了KeyDown="userPressEnter"

Now what I want to to, is make it simulate the TAB key, when in fact the RETURN gets hit. So in my WPF xaml I added imply KeyDown="userPressEnter"

及其背后的代码:

private void userPressEnter(object sender, KeyEventArgs e)
{
  if (e.Key == Key.Return)
  {
    e.Key = Key.Tab // THIS IS NOT WORKING
  }
}

现在,显然这是行不通的.但是我不知道,我该如何做这项工作?

Now, obviously this is not working. But what I don't know is, how DO I make this work?

编辑1 ==>找到解决方案

EDIT 1 ==> FOUND A SOLUTION

我找到了可以帮助我的东西=)

I found something that helped me out =)

private void userPressEnter(object sender, KeyEventArgs e)
{
 if (e.Key == Key.Return)
 {
   TraversalRequest request = new TraversalRequest(FocusNavigationDirection.Next);
   MoveFocus(request);
 }
}

通过这种方式,焦点可以移至下一个可以找到的位置:)

This way the Focus moves on the the next it can find :)

推荐答案

您可以在此处查看帖子: http://social.msdn.microsoft .com/Forums/en/wpf/thread/c85892ca-08e3-40ca-ae9f-23396df6f3bd

You can look at a post here: http://social.msdn.microsoft.com/Forums/en/wpf/thread/c85892ca-08e3-40ca-ae9f-23396df6f3bd

这是一个例子:

private void textBox1_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                TraversalRequest request = new TraversalRequest(FocusNavigationDirection.Next);
                request.Wrapped = true;
                ((TextBox)sender).MoveFocus(request);
            }
        }

这篇关于在WPF应用程序中按Return键时,如何模拟Tab键的按下?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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