如何删除除wpf中的一个以外的所有标签项?

编程入门 行业动态 更新时间:2024-10-28 15:30:34
本文介绍了如何删除除wpf中的一个以外的所有标签项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想在点击退出按钮时删除所有标签项。我的代码如下:

I want to remove all tab items when i click the logout button.My code is given below

foreach (object item in mainTab.Items) { TabItem ti = (TabItem)item; if ("welcomeTabItem" != ti.Name) { mainTab.Items.Remove(item); } }

但这会产生以下错误 error-Collection被修改;枚举操作可能无法执行。 是否存在任何其他方法 在预先感谢

But this will create a following error error-Collection was modified; enumeration operation may not execute. Is any other method is existing Thanks in Advance

推荐答案

你不能在 foreach 循环中更改集合。使用,或 you can''t change collection inside foreach loop. Use something like while or for

查看此链接 stackoverflow/ questions / 7272084 / closing-removal-a-tab-item-wpf [ ^ ]

这里的简易解决方案: Easy Solution here: IEnumerable<tabitem> items = mainTab.Items.Cast<TabItem>().Where(x => !x.Name.Equals("welcomeTabItem")); foreach (TabItem item in items.ToList()) mainTabs.Items.Remove(item);

首先转换为IEnumerable< TabItem>,之后你做一个Where排除的查询只有欢迎标签。从TabControl中删除所有项目后。

First cast as IEnumerable<TabItem>, after that you do a Where query that excludes only the welcome tab. After you remove all the items from the TabControl.

更多推荐

如何删除除wpf中的一个以外的所有标签项?

本文发布于:2023-06-06 05:51:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/534703.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:标签   wpf

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!