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

发布于 2024-08-22 00:14:06 字数 828 浏览 10 评论 0原文

我确信我错过了一些简单的东西,但我必须承认,在这一点上我不知所措。

我以编程方式将 TabItems 添加到我的主 TabControl,一个对应于用户选择打开的每个帐户。在创建和添加新的 TabItem 之前,我想检查用户是否已在另一个选项卡中打开该帐户。我不想最终打开两个相同的选项卡。

这是我最初编写的代码。希望它能让您了解我正在努力实现的目标。

    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() 方法正在寻找什么,因为它只指定一个对象作为参数,而不引用该项目的名称!

非常感谢任何和所有的帮助。

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

行至春深 2024-08-29 00:14:06

Contains() 方法正在寻找您传递您正在查找的实际 TabItem ,因此它不会帮助您。但这会起作用:

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

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

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
  ...
情场扛把子 2024-08-29 00:14:06

感谢您的回复!在编辑之前它不起作用,我最终提出了另一个类似的解决方案。当然让我朝着正确的方向思考!我仍然不太习惯 LINQ 和 lambda 表达式。

如果其他人正在寻找解决方案,这也对我有用:

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

如果有人正在阅读本文,最后一个问题...是否有一种更优雅的方法通过引用 name 属性来从matchingItem中选择元素,而不是假设正确的元素位于位置0?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文