在WPF应用程序中查找所有打开的弹出窗口
问题描述:
WPF具有Popup类,您可以通过该类在另一个窗口内打开一个(小)窗口.例如,这用于工具提示或组合框.
WPF has the Popup class with which you can open a (small) window inside another window. This is used for example for Tooltips or ComboBoxes.
我需要找到所有这些当前在WPF窗口中打开的弹出窗口,因此我可以将其关闭.
I need to find all of these popups which are currently opened inside a WPF window, so I can close them.
答
如果仍然需要:
public static IEnumerable<Popup> GetOpenPopups()
{
return PresentationSource.CurrentSources.OfType<HwndSource>()
.Select(h => h.RootVisual)
.OfType<FrameworkElement>()
.Select(f => f.Parent)
.OfType<Popup>()
.Where(p => p.IsOpen);
}