如何不处理已经由例如处理过的 KeyDown 路由事件。一个文本框
我在 WPF 中有一个附加了 KeyDown 事件处理程序的窗口。
当用户按下数字键 (0-9) 时,我会响应该事件。我使用 0-9 作为快捷键来选择单选按钮。例如。按 3 键将选择第三个单选按钮。
问题是,当用户在文本框中按下数字键时,我不想将按键作为快捷方式处理,因为我想保留文本框的默认实现。
现在,当用户在 TextBox 内按下 3 时,TextBox 的文本将设置为 3,同时也会选择第三个单选按钮。
我认为当用户在 TextBox 内按下按键时,TextBox 会将 e.Handled 设置为 true,但事实并非如此。
文本框也只是一个例子。如果用户在我的层次结构中的其他输入控件中输入某些内容,我不想响应 KeyDown 事件。
我想我需要更好地理解路由事件来解决这个问题,或者也许以另一种方式解决这个问题?
I have a Window in WPF with a KeyDown event handler attached.
When the user presses a numeric key (0-9) I respond to the event. I use the 0-9 as shortcut keys to select a radiobutton. Eg. pressing key 3 will select the 3'rd radiobutton.
The problem is that when the user presses a numeric key inside a TextBox I don't want to handle the keypress as a shortcut, because I want to keep the default implementation of the TextBox.
Right now when the user presses 3 when inside a TextBox, the text of the TextBox is set to 3, but also the 3'rd radiobutton is selected.
I thought the TextBox would set e.Handled to true when the user presses the key down inside the TextBox, but that isn't the case.
Also the TextBox is just an example. If the user enters something in other input controls in my hierarchy I don't want to respond to KeyDown events.
I guess I need a better understanding of routed events to solve this, or maybe solve this in another way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以检查
RoatedEventArgs
的OriginalSource
属性(在您的情况下为KeyPressEventArgs
)。如果是TextBox
,则不处理它。如果不是,请执行您的RadioButton
选择逻辑。You can check the
OriginalSource
property ofRoutedEventArgs
(KeyPressEventArgs
in your case). If it isTextBox
, don't handle it. If it is not, do yourRadioButton
selection logic.尝试使用 Form.
KeyPreview
属性并使用ActiveControl
属性来触发逻辑Try using the Form.
KeyPreview
property and also use theActiveControl
property to trigger the logic