在 WPF 文本框中使用箭头键 (C#) [英] Working with Arrow Keys in a WPF Textbox (C#)
问题描述
有什么方法可以捕捉 WPF 文本框 (System.Windows.Controls.Textbox
) 中的向上/向下箭头键并允许它们更改文本?我已经阅读了关于覆盖 Windows 窗体文本框 (System.Windows.Forms.TextBox
) 的 ProcessCmdKey
方法的信息,它运行良好,但是 TextBox
不如 WPF 灵活.有没有类似的方法可以在不使用旧的 Windows 窗体 TextBox 的情况下完成此操作?
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?
对于我的示例,我有一个带有数字文本掩码的 TextBox.我希望能够通过使用向上和向下箭头键来增加/减少数值.
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.
推荐答案
您可以将事件处理程序添加到 KeyUp 和/或 KeyDown,如果这不能满足您的需求,请使用 PreviewKeyUp 和/或 PreviewKeyDown.
You could add event handlers to KeyUp and/or KeyDown, if that doesn't get what you need, using PreviewKeyUp and/or PreviewKeyDown should.
这篇关于在 WPF 文本框中使用箭头键 (C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!