Search code examples
c#wpftextboxeventsrouted

C#/WPF: Catch Routed Event (UIElement.PreviewKeyUp) from UserControl


I've a UserControl which contains a TextBox called myTextBox. This UserControl is placed in a MainWindow.xaml where I'm trying to catch the "UIElement.PreviewKeyUp".

Now when debugging, I can see the "UserControl" as sender, not the TextBox. How can I check if the KeyEventArgs e (especially e.Key) were sent from the TextBox called myTextBox?

Thanks for any help.

Cheers


Solution

  • You can find it as the e.Source property. Try to cast it to 'TextBox', and if it succeeds, you can check the Name property for 'myTextBox'.

    [edited after comment]

    If the TextBox is inside a UserControl, you can use the OriginalSource property of the event.