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