WPF / C #: How is TabItems referenced inside TabControl?

I am sure that there is something simple that I am missing, but I must admit that at this moment I am at a loss.

I programmatically add TabItems to my main TabControl, one for each account that the user wants to open. Before creating and adding a new TabItem, I would like to check if the user is open on another tab. I don’t want the end to open two identical tabs.

Here is the code I originally wrote. Hope this gives you an idea of ​​what I'm 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 ContainsKey, TabPage. , Items.Contains(), !

.

!

+3
2

Contains() TabItem, , . :

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

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

! , . , ! LINQ .

, - , :

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

, - ... matchItem name , 0?

+1

Source: https://habr.com/ru/post/1733093/


All Articles