如何删除除wpf中的一个以外的所有标签项? [英] How to remove all tab items except one in wpf ?

查看:92
本文介绍了如何删除除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

查看此链接



http://stackoverflow.com/ 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中的一个以外的所有标签项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆