查找和关闭 WPF 窗口的所有子窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12981497/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Finding and Closing All Child Windows of a WPF Window
提问by Bryan
Ok so I have a WPF application that has a WinForms project in it. My main project is the WPF one. My question is how do I find all of the widows that are children of my Main Window that is WPF?? These Windows can be either WinForm or WPF so i need a way to determine of which type the child is to successfully close said form and all of its potential children.
好的,所以我有一个 WPF 应用程序,其中包含一个 WinForms 项目。我的主要项目是 WPF 之一。我的问题是如何找到作为 WPF 主窗口的所有孩子的寡妇?这些 Windows 可以是 WinForm 或 WPF,因此我需要一种方法来确定子级成功关闭所述窗体及其所有潜在子级的类型。
Thank you in advance for the assistance
预先感谢您的帮助
回答by Henk Holterman
I have to guess a little but I think your WinForms forms aren't actually children of the WPF MainWindow. I don't see how they could be.
我不得不猜测一下,但我认为您的 WinForms 表单实际上并不是 WPF MainWindow 的子级。我不明白他们怎么会这样。
So they're probably just other non-main Windows in your application, not proper children.
因此,它们可能只是您的应用程序中的其他非主 Windows,而不是真正的子级。
That means you have 2 collections to look at:
这意味着您要查看 2 个集合:
- WinForms :
System.Windows.Forms.Application.OpenForms - WPF :
System.Windows.Application.Current.Windows
- WinForms :
System.Windows.Forms.Application.OpenForms - WPF :
System.Windows.Application.Current.Windows
Or maybe MainWindow.OwnedWindowsfor the WPF side.
或者也许MainWindow.OwnedWindows是 WPF 方面。

