相关文章推荐
热心的茶壶  ·  如何使用KnexJS插入对象?-腾讯云开发者 ...·  2 年前    · 
酒量大的火龙果  ·  未能加载文件或程序集“Newtonsoft. ...·  2 年前    · 
傲视众生的佛珠  ·  Python连接impala并获取数据-腾讯 ...·  3 年前    · 
安静的豆浆  ·  string - ...·  3 年前    · 
Code  ›  C# WPF如何关闭通过父窗口打开的所有子窗口开发者社区
wpf
https://cloud.tencent.com/developer/article/1895642
小眼睛的鼠标垫
3 年前
作者头像
用户9127601
0 篇文章

C# WPF如何关闭通过父窗口打开的所有子窗口

前往专栏
腾讯云
开发者社区
文档 意见反馈 控制台
首页
学习
活动
专区
工具
TVP
文章/答案/技术大牛
发布
首页
学习
活动
专区
工具
TVP
返回腾讯云官网
社区首页 > 专栏 > dotNET编程大全 > C# WPF如何关闭通过父窗口打开的所有子窗口

C# WPF如何关闭通过父窗口打开的所有子窗口

作者头像
用户9127601
发布 于 2021-11-01 16:48:09
1.2K 0
发布 于 2021-11-01 16:48:09
举报
foreach (Window item in Application.Current.Windows)
    if (item.Title == "") continue; // 跳过无标题窗口
  if (item.Title != "父窗口标题") item.Close();
}

WinForm时这样操作的:

// 遍历并关闭所有子窗口
FormCollection childCollection = Application.OpenForms;
for (int i = childCollection.Count; i-- > 0;)
  if (childCollection[i].Name != "父窗口标题") childCollection[i].Close();
// 或者也可以这样写:
FormCollection childCollection = Application.OpenForms;
for (int i = childCollection.Count; i-- > 0;)
  if (childCollection[i].Name != this.Title) childCollection[i].Close();

WPF中时这样操作的:

// 遍历并关闭所有子窗口
Window[] childArray = Application.Current.Windows.Cast<Window>().ToArray();
for (int i = childArray.Length; i-- > 0;)
  Window item = childArray[i];
  if (item.Title == "") continue; // 忽略无标题窗口
  if (item.Title != "父窗口标题") item.Close();
// 或者也可以这样写:
Window[] childArray = Application.Current.Windows.Cast<Window>().ToArray();
for (int i = childArray.Length; i-- > 0;)
 
推荐文章
热心的茶壶  ·  如何使用KnexJS插入对象?-腾讯云开发者社区-腾讯云
2 年前
酒量大的火龙果  ·  未能加载文件或程序集“Newtonsoft.Json”或它的某一个依赖项。找到的程序集清单定义与程序集引用不匹配。-CSDN博客
2 年前
傲视众生的佛珠  ·  Python连接impala并获取数据-腾讯云开发者社区-腾讯云
3 年前
安静的豆浆  ·  string - System.JSONException: Unexpected character ('i' (code 105)): was expecting comma to separate OBJECT entries at [line:1, c
3 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号