【问题标题】:Search a collection of TabItems for a TabItem Name在 TabItem 集合中搜索 TabItem 名称
【发布时间】:2016-10-05 11:29:40
【问题描述】:

我想将 TabItem 的名称输入到包含 TabItem 集合的 TabControl 的窗口中,以编程方式搜索集合并打开名称与该输入匹配的 TabItem。 dkozl 在 2013 年 8 月 16 日回答了一个类似的问题,但我不明白(我是新手)。

我已经解决了几天,并提出了以下建议(不起作用)

foreach (IEnumerable<TabItem> item in tabControlList)
{
    if (item.Name == "AddRskAreas")
    {
        item.IsSelected = true;
    }         
    else
    {
        MessageBox.Show("Tab not found");
    }
}

我很难理解如何实现 IEnumerable。谁能帮我解决这个问题?

【问题讨论】:

  • doesn't work 是您可以对问题给出的最无用的解释。当你去汽车修理店时,你会告诉他们“它不起作用”吗?请解释从上面的代码中收到的错误是什么,并为此 foreach 添加更多上下文。什么是以及如何声明和初始化变量 tabControlList
  • 您还应该查看调试,在循环中放置一个断点。看看 item.Name 的值是多少。查看项目上的所有值。这就是我发现选项卡的名称在“标题”属性中的方式。
  • 感谢大家的意见。我已经找到了一个可行的解决方案并将其发布在下面,以防其他人可以使用它。

标签: c# wpf


【解决方案1】:

您可以使用 LINQ 更新列表中的多个值:

tabControlList.Where(item => item.Name == "AddRskAreas").ToList().ForEach(item => item.IsSelected = true);

【讨论】:

    【解决方案2】:

    您需要实际比较“标题”而不是名称。

    foreach (IEnumerable<TabItem> item in tabControlList)
    {
        if (item.Header== "AddRskAreas")
        {
            item.IsSelected = true;
        }         
        else
        {
            MessageBox.Show("Tab not found");
        }
    }
    

    【讨论】:

      【解决方案3】:

      在 Sean Sextons 的 2,000 件关于 C# 知识库中你应该知道的事情的帮助下,我得出了一个解决方案。我将在下面发布我的解决方案的关键 XAML 和 C# 片段,以防其他人将它们拼凑在一起并从中获得价值。

      有趣的是,按钮现在淡出 - 有人知道为什么吗?

      //XAML 片段 ....

          <!-- These RoutedUICommands (in <Window.Resources>) are bound to the Process Procedure Selection Buttons.  Clicking on the button 
          opens the corresponding process procedure TabItem  -->
          <RoutedUICommand x:Key="OpenPrcdrTbItm" Text="This Command opens the Process Procedure TabItem"/>
      
      <Window.CommandBindings>
          <!-- These CommandBindings (in <Window.CommandBindings>) bind the Process Procedure Selection Button Commands to the Command Handler in the code behind  -->
          <CommandBinding Command = "{StaticResource OpenPrcdrTbItm}" Executed="RskPrcssPrcdrs_Click"/>
      
          <Button x:Name="ChngeRskAreas" Grid.Column="1"  Content="Change Risk Areas"
          Command ="{StaticResource OpenPrcdrTbItm}" CommandParameter="ChngeRskAreas"/>
      

      //片段背后的C#代码

          //Select the chosen TabItem 
          public void RskPrcssPrcdrs_Click(object sender, ExecutedRoutedEventArgs e)
          {
              RskManWndw rskManWndw = new RskManWndw(this);      //Instantiate a new rskManWndw window
              TabControl tabControlCollection = new TabControl();
              TabItem tabItemCollection = new TabItem();
              string slctdTabItem = (string)e.Parameter;
              bool slctdTabItemFnd = false;
              string msgBoxMsg = "";
      
              //Open the rskAraManWndw window
              rskManWndw.Show();
              foreach (TabItem tabItem in rskManWndw.RskManPrcssTbCtl.Items)
              {
                  if (tabItem.Name == slctdTabItem)
                  {
                      tabItem.IsSelected = true;                          //Select the chosen TabItem.
                      slctdTabItemFnd = true;                             //Flag that the TabItem was found.
                      break;
                  }
              }
              if (slctdTabItemFnd == false)                               //Was the TabItem found?
              {
                  msgBoxMsg = "A TabItem matching the" + slctdTabItem + "Command Parameter was not found.  "
                            + "Please inform the system administrator.";
                  MessageBox.Show($" {msgBoxMsg}", "RMS Processing Error Alert");
                  rskManWndw.Close();
              }
              else
              {
                  Hide();                                                     //Hide the Risk_Management_System.MainWindow
              }
          }
      

      【讨论】:

        猜你喜欢
        • 2021-10-10
        • 1970-01-01
        • 2011-06-05
        • 2014-04-16
        • 2013-10-02
        • 1970-01-01
        • 2017-11-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多