如果设置为true,AcceptsReturn禁用多行文本框Key.Return的检测? [英] If set to true, AcceptsReturn disables detection of Key.Return in a multiline textbox?

查看:406
本文介绍了如果设置为true,AcceptsReturn禁用多行文本框Key.Return的检测?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Windows应用程序,通过我AcceptsReturn属性设置为True做了多行文本框。它允许用户输入多行文本到文本框中。另外,我想每一次做某件事,回车/键在文本框中输入压制。事件处理程序代码如下...

In my Windows application, I made a multiline textbox by setting AcceptsReturn property to True. It lets the user enter multiple lines of text into the textbox. Also, I'd like to do something every time, the Return/Enter key is pressed in the textbox. The event handler code is as follows...

private void TextBox_KeyDown(object sender, KeyEventArgs e)
{ 
    if (e.Key == Key.Return)

        // do something here
}

看来,如果AcceptsReturn设置为True,并且回车键被按下,这个事件处理函数不是在所有调用。任何其他按键被正确地检测。如果AcceptsReturn未设置为True,并且回车键被按下时,事件处理函数和返回按键被检测就好了。这里的问题是,按回车键并在用户不提前在文本框输入新线(如预期)。
所以,我想回车按键给用户正确地前进到文本框输入新的产品,以及我希望能够检测到返回按键。是我的方法不对?有没有更好的方式来做到这一点?

It appears that, if AcceptsReturn is set to True and the Return key is pressed, this event handler is not called at all. Any other key press is detected properly. If AcceptsReturn is not set to True and the Return key is pressed, the event handler is called and Return key press is detected just fine. The problem with this is that pressing Return key doesn't advance the user to the new line in the textbox (as expected). So, I'd like the Return key press to properly advance the user to the new line in the textbox as well as I'd like to be able to detect that Return key press. Is my approach wrong? Is there a better way to do this?

推荐答案

的KeyDown 冒泡事件,这意味着它是首次提出对源控件(文本框),然后在父母,然后在父亲的父亲,等等,直到它被处理。当您设置 AcceptsReturn 为true,则控制手柄返回键,因此不冒泡的事件。在这种情况下,你可以使用事件的隧道版本: PreviewKeyDown ,这是从顶部到达源代码控制前下方控制的每个祖先提高。

KeyDown is bubbling event, which means it is first raised on the source control (the TextBox), then on the parent, then on the parent's parent, and so on, until it is handled. When you set AcceptsReturn to true, the control handles the Return key, so the event is not bubbled. In this case you can use the tunneling version of the event: PreviewKeyDown, which is raised on each ancestor of the control from the top to the bottom before it reaches the source control.

请参阅路由策略在MSDN上

这篇关于如果设置为true,AcceptsReturn禁用多行文本框Key.Return的检测?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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