I have a Page with a UserControl on it. If the user presses Esc while anywhere on Page I want to handle.
I thought this would be as easy as hooking up the PreviewKeyDown event, testing for the Esc key, and then handling it. However, when I placed I breakpoint in the event handler I found it was never getting called. I thought perhaps the UserControl might be getting hit, so I tried PreviewKeyDown there… same result.
Does anyone know the proper place to test for a KeyDown or PreviewKeyDown on a Page object?
Best Answer
Attach to the Window's Event
After the control is loaded, attach to the Window's
KeyDown
event (or any event) by usingWindow.GetWindow(this)
, like so:The XAML
The Code Behind