WPF / C#:如何做一个TabControl内的一个参考的TabItems? [英] WPF/C#: How does one reference TabItems inside a TabControl?

查看:640
本文介绍了WPF / C#:如何做一个TabControl内的一个参考的TabItems?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我敢肯定有一些简单的我失踪,但我必须承认,在这一点上,我很茫然。

I'm sure there is something simple that I am missing, but I must confess that at this point I am at a loss.

我编程加入的TabItems我的主要的TabControl,每个帐户的用户选择打开。创建和添加新的TabItem的之前,我想检查,如果用户已经在其他标签页中打开的帐户。我不想有两个相同的标签打开就结了。

I am programmatically adding TabItems to my main TabControl, one for each account that the user chooses to open. Before creating and adding a new TabItem I would like to check if the user already has the account open in another tab. I do not want to end up with two identical tabs open.

下面是我原来写的代码。希望它给你什么,我试图完成一个想法。

Here is the code that I originally wrote. Hopefully it gives you an idea of what I am trying to accomplish.

    if (tab_main.Items.Contains(accountNumber))
    {
        tab_main.SelectedIndex = tab_main.Items.IndexOf(accountNumber);
    }
    else
    {
        Search s = new Search(queryResults, searchText);
        TabItem tab_search = new TabItem();
        tab_search.Header = searchString;
        tab_search.Name = accountNumber;
        tab_search.Content = s;
        tab_main.Items.Add(tab_search);
    }



当然,这并不正常工作。在的WinForms TabControl的将有一个集合的TabPages用,我可以用它来搜索的TabPage名称的ContainsKey方法。我没有得到什么Items.Contains()方法是寻找,因为它只规定了一个对象作为参数,并没有引用该项目的名称!

Of course this doesn't work properly. In WinForms the TabControl has a TabPages collection with a ContainsKey method that I could use to search for the name of the TabPage. I don't get what the Items.Contains() method is looking for since it only specifies an object as an argument and doesn't reference the item's name!

任何和所有帮助是极大的赞赏。

Any and all help is greatly appreciated.

谢谢!

推荐答案

包含()方法是找你在通过实际的 TabItem的您正在寻找的,所以不会帮你。但是,这将工作:

The Contains() method is looking for you to pass in the actual TabItem you are looking for, so it won't help you. But this will work:

var matchingItem =
  tab_main.Items.Cast<TabItem>()
    .Where(item => item.Name == accountNumber)
    .FirstOrDefault();

if(matchingItem!=null)
  tab_main.SelectedItem = matchingItem;
else
  ...

这篇关于WPF / C#:如何做一个TabControl内的一个参考的TabItems?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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