从TabItem获取并迭代控件?

时间:2012-03-20 09:16:54

标签: c# wpf tabcontrol tabitem

如何获取嵌套在Tabitem中的所有控件/ UIElements(来自TabControl)?

我尝试过所有东西,但却无法得到它们。

(设置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之后: 这样做,Count总是1:

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

3 个答案:

答案 0 :(得分:3)

TabControl具有Items属性(派生自ItemsControl),它返回所有TabItems - http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.items.aspx。或者你可以遍历可视树:

var firstStackPanelInTabControl = FindVisualChildren<StackPanel>(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;
    }
  }
}

答案 1 :(得分: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

答案 2 :(得分: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;
    }