在TabItems的集合中搜索TabItem名称

时间:2016-09-26 13:42:11

标签: c# wpf

我想将TabItem的名称输入到包含TabControl的Window,该TabControl包含TabItems的集合,以编程方式搜索集合并打开名称与该输入匹配的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。有人可以帮我这个吗?

3 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

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

<?php

    if(isset($_FILES['img'])){
      foreach ($_FILES['img'] as $img) {
        $name_array[] = $img['name'];
        $tmp_name_array[] = $img['tmp_name'];
        $type_array[] = $img['type'];
        $size_array[] = $img['size'];
        $error_array[] = $img['error'];
        for($i = 0; $i < count($tmp_name_array); $i++){
            if(move_uploaded_file($tmp_name_array[$i], "pics/".$name_array[$i])){
                echo $name_array[$i]." upload is complete<br>";
            } else {
                echo "move_uploaded_file function failed for ".$name_array[$i]."<br>";
            }
        }
      }
    }

    ?>

答案 2 :(得分:0)

我在Sean Sextons的帮助下得到了一个解决方案,你需要了解关于C#知识库的2000件事。我将在下面发布我的解决方案的关键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
        }
    }