Search code examples
wpfroutedevents

WPF - Handling a RoutedEvent in the parent for a specific control instance


In my user control I have several textboxes e.g. Textbox1, Textbox2.

In the parent I only want to handle KeyDown events raised from Textbox1, not all Textbox

I have added a handler in the parent

this.AddHandler(TextBox.KeyDownEvent, new RoutedEventHandler(OnTextboxGoToPageKeyDown));

but of course this will handle all KeyDown events which I don't want.

Do I have to add some conditional logic in the RoutedEventHandler to check where the event was raised from?

or is there a better way?

What's the best way to do this?

Thanks.


Solution

  • The RoutedEventArgs in your OnTextboxGoToPageKeyDown has a property named Source which have a references to the Object who raised the event. You can ask for that property when you need to execute your logic.

    HTH