A Microsoft platform for building and publishing apps for Windows devices.
Hi @Harshithraj1871 ,
Thanks for including the element tree and the handler order. That made it easy to reproduce.
The order you're seeing is expected. UIElement.PreviewKeyDown is documented as a tunneling event whose bubbling counterpart is KeyDown, and PreviewKeyUp pairs with KeyUp the same way. So the preview events travel from RootGrid down to the focused TextBox, and then KeyDown and KeyUp bubble back up.
The docs don't state the design reason, but they do explain what the preview phase is for, and that points to the most likely reason. Keyboard input targets the focused element, and controls often act on a key before their ancestors get a chance to see it. According to the Advanced Concepts section of Keyboard accelerators, the preview events fire before the focused element's accelerators, its OnKeyDown class handling, and the bubbling KeyDown. Keyboard events also notes that TextBox marks keys like the arrow keys as handled when it uses them, so a normal KeyDown handler on an ancestor doesn't run. Tunneling gives a container like your RootGrid or Panel the first look at the key, before the focused control acts on it.
I tested this with your tree in a C++/WinRT app on Windows 11 with Windows App SDK 1.7.250909003 and 2.7.0. When I pressed Left in the TextBox while it had text, PreviewKeyDown ran on RootGrid → Panel → Input, but none of the normal KeyDown handlers ran, so the ancestors only saw that key through the preview event. This matches the API reference, even though the Advanced Concepts section mentioned above describes the preview events as bubbling.
As for why pointer and other routed events don't have preview versions, I couldn't find anything in the WinUI docs that explains it, so what follows is my own reading of the documentation rather than an official explanation. The docs tie the preview pair to keyboard-specific scenarios: a keyboard event starts from whichever element has focus, and the focused control may consume the key for its own behavior or for accelerators before any ancestor sees it. Because of that, a container that needs to react to keys, for example for its own shortcuts or navigation, needs a way to see the key before the focused control does. My guess is that this is why preview routing was added only for keyboard input. I'd still treat these two pairs as a keyboard-specific part of the routing contract rather than assume the Preview prefix means tunneling anywhere else.
I hope the documentation and the test results above help clarify what you are seeing. If anything does not match on your side, feel free to share your Windows App SDK version and the key you are testing with. If the answer covers what you needed to confirm, you may refer to this guide if you find it appropriate. Should you later come across an official explanation of why only the keyboard events have a tunneling counterpart, adding it here could also help others who have the same question.
Thank you.