2016-09-26 64 views
0

我想輸入一個TabItem的名稱到一個包含一個TabControl的窗口,該TabControl包含一個TabItem集合,以編程方式搜索集合並打開與該輸入匹配的名稱的TabItem。 dkozl在2013年8月16日回答了一個類似的問題,但我不明白(我是新手)。搜索TabItem名稱的集合

我已經戒了這個繞了幾天,想出了以下(不工作)

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

我很難理解如何實現IEnumerable。任何人都可以幫助我嗎?

+0

_doesn't WORK_是,你可以給一個問題最沒用的解釋。當你去汽車修理店時,你是否告訴他們「這行不通」?請解釋從上面的代碼中收到的錯誤是什麼,併爲此foreach添加更多的上下文。什麼是和如何聲明和初始化變量_tabControlList_? – Steve

+0

你也應該看看調試,在你的循環中放置一個斷點。看看item.Name的值是什麼。查看項目上的所有值。這是我如何發現標籤的名稱是在「Header」屬性中。 – Derek

+0

謝謝大家的意見。我已經到達了一個可行的解決方案,並將其發佈在下方,以防其他人可以使用它。 – Joe

回答

0

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

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

你需要實際的比較「頁眉」,而不是名字。

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

我得出與肖恩教堂司事2000件事情你應該知道的關於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 
     } 
    }