开发者

Route KeyUpEvent from UIElement to a TextBox

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜