2012-03-20 72 views
3

如何獲取嵌套在Tabitem中的所有控件/ UIElements(來自TabControl)?通過TabItem控件獲取和迭代?

我嘗試了一切,但無法得到它們。

(設置SelectedTab):

private TabItem SelectedTab = null; 
    private void tabControl1_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     SelectedTab = (TabItem)tabControl1.SelectedItem; 
    } 

現在我需要這樣的:

private StackPanel theStackPanelInWhichLabelsShouldBeLoaded = null; 
    foreach (Control control in tabControl.Children /*doesnt exist*/, or tabControl.Items /*only TabItems*/, or /*SelectedTab.Items ??*/) //I Have no plan 
    { 
     if(control is StackPanel) 
     { 
      theStackPanelInWhichLabelsShouldBeLoaded = control; 
      //Load Labels in the Stackpanel, thats works without problems 
     } 
    } 

後Silvermind: 這樣做,伯爵始終是1:

 UpdateLayout(); 
     int nChildCount = VisualTreeHelper.GetChildrenCount(SelectedTab); 
+0

的TabItem的可能需要,他使這孩子之前先觸發UpdateLayout請。這將按照每個示例在選擇/打開時發生。佈局渲染後,您需要使用SelectedTab而不是它的Children。 – Silvermind 2012-03-20 09:30:07

+0

編輯答案..其仍然總是1(網格) – eMi 2012-03-20 09:36:58

回答

3

TabControl有Items屬性(從ItemsControl派生),它返回所有的TabItems - http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.items.aspx。或者你可以遍歷視覺樹:

var firstStackPanelInTabControl = FindVisualChildren(tabControl).First();

public static IEnumerable<T> FindVisualChildren<T>(DependencyObject rootObject) where T : DependencyObject 
{ 
    if (rootObject != null) 
    { 
    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(rootObject); i++) 
    { 
     DependencyObject child = VisualTreeHelper.GetChild(rootObject, i); 

     if (child != null && child is T) 
     yield return (T)child; 

     foreach (T childOfChild in FindVisualChildren<T>(child)) 
     yield return childOfChild; 
    } 
    } 
} 
+0

我不需要TabItems,我需要從TabItem本身,它嵌套在TabItem或TabControl中的控件/ UIElements,我dk – eMi 2012-03-20 09:25:30

+0

然後,你總是可以只是遍歷視覺樹。我將示例代碼添加到答案 – Nikolay 2012-03-20 09:32:46

+0

現在工作,謝謝 – eMi 2012-03-20 09:43:14

2

可能這種方法會幫助你:

public static IEnumerable<T> FindChildren<T>(this DependencyObject source) 
              where T : DependencyObject 
{ 
    if (source != null) 
    { 
    var childs = GetChildObjects(source); 
    foreach (DependencyObject child in childs) 
    { 
     //analyze if children match the requested type 
     if (child != null && child is T) 
     { 
     yield return (T) child; 
     } 

     //recurse tree 
     foreach (T descendant in FindChildren<T>(child)) 
     { 
     yield return descendant; 
     } 
    } 
    } 
} 

查看完整的文章(查找元素在WPF樹)here

+0

這也可以,謝謝 – eMi 2012-03-20 09:45:12

0

我VisualTreeHelper.GetChildrenCount總是標籤控件返回0,我只好用這種方法,而不是

public static List<T> ObtenerControles<T>(DependencyObject parent) 
    where T : DependencyObject 
    { 
     List<T> result = new List<T>();   
     if (parent != null) 
     { 
      foreach (var child in LogicalTreeHelper.GetChildren(parent)) 
      {     
       var childType = child as T; 
       if (childType != null) 
       { 
        result.Add((T)child); 
       } 

       foreach (var other in ObtenerControles<T>(child as DependencyObject)) 
       { 
        result.Add(other); 
       } 
      } 
     } 

     return result; 
    }