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

孤者浪人 提交于 2019-12-10 20:24:01

问题


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. 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);
    }

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.

Thanks!


回答1:


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
  ...



回答2:


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.

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?



来源:https://stackoverflow.com/questions/2277464/wpf-c-how-does-one-reference-tabitems-inside-a-tabcontrol

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!