在WPF文本框(c#)中使用箭头键

时间:2022-01-16 17:27:20

Is there any way to catch the Up/Down arrow keys in a WPF TextBox (System.Windows.Controls.Textbox) and allow them to alter the text? I've read about overiding the ProcessCmdKey method for a Windows Forms TextBox (System.Windows.Forms.TextBox), and it worked fine, but that TextBox is not nearly as flexible as the WPF one. Is there a similar method to accomplish this without having to use the old Windows Forms TextBox?

有什么方法可以捕捉WPF文本框(System.Windows.Controls.Textbox)中的向上/向下箭头键并允许它们修改文本吗?我读到过关于重写Windows窗体文本框(System.Windows.Forms.TextBox)的ProcessCmdKey方法,它工作得很好,但是这个文本框远没有WPF那么灵活。是否有类似的方法可以在不使用旧的Windows窗体文本框的情况下实现这一点?

For my example, I have a TextBox that has a numeric text-mask. I want to be able increase/decrease the numeric value by using the up and down arrow keys.

对于我的示例,我有一个带有数字文本遮罩的文本框。我想通过上下箭头键来增加/减少数值。

1 个解决方案

#1


26  

You could add event handlers to KeyUp and/or KeyDown, if that doesn't get what you need, using PreviewKeyUp and/or PreviewKeyDown should.

您可以将事件处理程序添加到KeyUp和/或KeyDown中,如果这不能得到您所需要的,那么可以使用PreviewKeyUp和/或PreviewKeyDown。

#1


26  

You could add event handlers to KeyUp and/or KeyDown, if that doesn't get what you need, using PreviewKeyUp and/or PreviewKeyDown should.

您可以将事件处理程序添加到KeyUp和/或KeyDown中,如果这不能得到您所需要的,那么可以使用PreviewKeyUp和/或PreviewKeyDown。