Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如何在 WPF 中将a 的KeyUpEvent事件路由UIElement到 a ?TextBox
KeyUpEvent
UIElement
TextBox
例如以下对象:
<Rectangle x:Name="rectangleWPF"></Rectangle> <TextBox x:Name="textBoxWPF"></TextBox>
如果在rectangleWPF上按下“A”,则必须将“A”插入到textBoxWPF中。然后,如果在rectangleWPF上按下退格键,textBoxWPF应该什么也不显示。
想得简单:在您的事件处理程序中UIElement,只需测试以查看按下了哪个键。如果是字母数字,请将其添加到textBoxWPF.Text属性中。如果是退格,则使textBoxWPF.Text属性成为其自身的子字符串,减去最后一个字符。如果这是您不想要(或不处理)的其他东西,请忽略它。
textBoxWPF.Text
当简单的事情会更好地工作时,没有理由实施一些魔术来重新路由事件。