开发者

Route KeyUpEvent from UIElement to a TextBox

开发者 https://www.devze.com 2023-01-19 18:49 出处:网络
How do I route the KeyUpEvent event for a UIElement to a TextBox in WPF? For example with the following objects:

How do I route the KeyUpEvent event for a UIElement to a TextBox in WPF?

For example with the following objects:

<Rectangle x:Name="rectangleWPF"></Rectangle>
<TextBox x:Name="textBoxWPF"></TextBox>

If an 'A' is pressed on rectangleWPF then an 'A' must be inserted into textBoxWPF. 开发者_开发知识库 Then if a backspace is pressed on rectangleWPF, textBoxWPF should display nothing.


Think simple: in your event handler for the UIElement, just test to see which key is pressed. If it's alphanumeric, add it to the textBoxWPF.Text property. If it's a backspace, make the textBoxWPF.Text property a substring of itself, minus the last character. If it's something else you don't want (or don't handle), ignore it.

There's no reason to implement some magic to reroute events when something simple will work better.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号