tags:

views:

19

answers:

1

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.

A: 

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.

Wonko the Sane