UWP: How to fix losing focus when a grid or other non-focusable control is clicked
Ever noticed a situation where, in your UWP app, the user clicks an empty area in a page or other non-focusable control, and then all of a sudden the whole page loses focus, completely breaking any KeyDown or PreviewKeyDown events you've set up? Never remember this happening with WPF and Windows Forms? That's because this is new behaviour with UWP - where, if you click on an empty space, focus will go all the way back to the root ScrollViewer which houses all your content - this ScrollViewer is provided by the platform and not something you can control. Luckily, this issue can be easily fixed by overriding the page's pointer event handling. This was reported here and fixed in WinUI 3, but not UWP. The fix To fix this, we can override the page's handling of pointer events, to stop the default behaviour where it passes focus on to the root ScrollViewer. Let's first add the event handler for the PointerPressed event and set a background (note that you can set the back...