如何根据名称或标题删除选项卡项 [英] How to delete a Tab Item based off of it's name or header

查看:32
本文介绍了如何根据名称或标题删除选项卡项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题类似于这个问题 我今天早些时候问过.不同的是,现在我想删除由它的 nameheader 引用的 Tab Item.我能否以类似于我在这个问题上得到的答案的方式调用 Remove?

This question is similar to this question I asked earlier today. The difference is, now I would like to delete a Tab Item referenced by it's name or header. Can I call Remove in a fashion similar to the answer I got on this question?

这是我试过的:

tabControl.Items.Remove = tabControl.Items //Changes tab according to TreeView
                        .OfType<TabItem>().SingleOrDefault(n => n.Name == stringValue);

我可以使用这个之类的东西吗?如果是这样,如何?

Can I use something like this? If so, how?

推荐答案

我不太了解如何从 wpf 中删除,但是此代码比您发布的更有效.remove 是一个方法,你不能给它赋值,所以你必须隔离你要删除的项目,检查以确保它不为空,然后将对象传递到 Remove方法.

I don't know much about removing from wpf, however this code is way more likely to work than what you have posted. Remove is a method, you can't assign it a value, so you have to isolate the item you want to remove, check to make sure it isn't null, then pass the object into the Remove method.

var tabToDelete = tabControl.Items.OfType<TabItem>().SingleOrDefault(n => n.Name == stringValue);
if (tabToDelete != null) // Since you chose to use SingleOrDefault, we have to check to make sure it isn't null before we try to remove it.
tabControl.Items.Remove(tabToDelete);

但是,我强烈建议您查看 WPF - 从 ItemsSource 中删除项目的最佳方法,因为它详细介绍了检查项目是否可以删除,即使 Remove 方法可用于该控件.

However, I strongly suggest you take a look at WPF - Best way to remove an item from the ItemsSource since it goes into details about checking IF the item CAN be removed, and even if the Remove method is available to that control.

这篇关于如何根据名称或标题删除选项卡项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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