I want to cause all open popups (with StaysOpen == false) to close from code. Basically I want to simulate the user clicking the mouse (which would close the popups) from code.
I don’t need to actually simulate the click, I just need the resulting behavior. I’ve thought about just going through the visual tree looking for popups and closing each one, but that doesn’t seem like the cleanest approach.
Thanks in advance for any help or opinions.
A WPF popup actually creates a new window (a Win32 window, not a WPF
Windowinstance). So you can’t find it in theApplication.Windowscollection, but you can probably find it using a Win32 API likeEnumChildWindows.Once you have the handle, you can retrieve the associated
HwndSource. I think theRootVisualof theHwndSourceis thePopup(didn’t check, you might have to look deeper in the visual tree).So the code should be similar to this (completely untested):