'WPF Popup steals focus from owner
We have a WPF popup that is working perfectly except that when it opens it takes the focus from the window that triggered it which has a jarring effect as the color changes (to represent being inactive). Is there any way to alter this behavior? Or is there an easy way to disable the focus visual style only in this scenario?
Solution 1:[1]
Well if I recall it correctly, you could set the IsHitTestVisible
property of the root element of your popup to false
. E.g.
<Grid IsHitTestVisible="False">
<!-- all childs cannot get focus -->
</Grid>
At least in Silverlight, this was a proper workaround.
Solution 2:[2]
If you don't need to be able to navigate to your PopUp buttons with the keyboard you can set Focusable="False" on the Popup. This will still allow you to click on the buttons, but as I say, navigating to the buttons with the keyboard will not be possible.
Solution 3:[3]
You can subclass Popup
and override OnPreviewGotKeyboardFocus
to prevent the popup gaining focus:
protected override void OnPreviewGotKeyboardFocus(KeyboardFocusChangedEventArgs e)
{
// Disallow the popup to gain focus
e.Handled = true;
}
This seems to work well for my purposes.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | DHN |
Solution 2 | Richard E |
Solution 3 | Drew Noakes |