0

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.

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Jannes
  • 1
  • maybe you need to change the example... why adding a handler to rectangleWPF.PreviewKeyUp() with the logic for txtBoxWpf doesn't work for you? You want the Rectangle.KeyUp to fire TextBox.KeyUp? – Svetlozar Angelov Oct 08 '10 at 14:50

1 Answers1

0

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.

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Wonko the Sane
  • 10,623
  • 8
  • 67
  • 92