简体   繁体   English

WPF / C#:如何在TabControl中引用TabItems?

[英]WPF/C#: How does one reference TabItems inside a TabControl?

I'm sure there is something simple that I am missing, but I must confess that at this point I am at a loss. 我确信有一些我想念的简单,但我必须承认,此时我不知所措。

I am programmatically adding TabItems to my main TabControl, one for each account that the user chooses to open. 我以编程方式将TabItems添加到我的主TabControl,每个用户选择打开一个帐户。 Before creating and adding a new TabItem I would like to check if the user already has the account open in another tab. 在创建和添加新的TabItem之前,我想检查用户是否已在另一个选项卡中打开该帐户。 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);
    }

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. 在WinForms中,TabControl有一个带有ContainsKey方法的TabPages集合,我可以用它来搜索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! 我没有得到Items.Contains()方法正在寻找的东西,因为它只指定一个对象作为参数,并且不引用该项目的名称!

Any and all help is greatly appreciated. 非常感谢任何和所有的帮助。

Thanks! 谢谢!

The Contains() method is looking for you to pass in the actual TabItem you are looking for, so it won't help you. Contains()方法正在寻找您传递您正在寻找的实际TabItem ,因此它不会帮助您。 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
  ...

Thanks for the replies! 谢谢你的回复! Before the edit it didn't work and I ended up coming up with another similar solution. 在编辑之前它没有用,我最终提出了另一个类似的解决方案。 Certainly got me thinking in the right direction! 当然让我思考正确的方向! I'm still not quite used to LINQ and lambda expressions. 我还不太熟悉LINQ和lambda表达式。

In case anyone else is looking for solutions this also worked for me: 如果其他人正在寻找解决方案,这对我也有用:

var matchingItem = 
    from TabItem t in tab_main.Items where t.Name == searchHash select t;

if (matchingItem.Count() != 0)
    tab_main.SelectedItem = matchingItem.ElementAt(0);
else
    ...

One final question if anyone is reading this... is there a more elegant way to select the element from matchingItem by referencing the name property versus assuming the correct element is at position 0? 最后一个问题,如果有人正在阅读这个...是否有更优雅的方法从matchItem通过引用name属性与假设正确的元素位于0位置来选择元素?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM