C# 从TabItem获取并遍历控件?

C# 从TabItem获取并遍历控件?,c#,wpf,tabcontrol,tabitem,C#,Wpf,Tabcontrol,Tabitem,如何(从TabControl)获取嵌套在Tabitem中的所有控件/UI元素 我什么都试过了,但没能得到 (设置所选选项卡): 现在我需要这样的东西: private StackPanel theStackPanelInWhichLabelsShouldBeLoaded = null; foreach (Control control in tabControl.Children /*doesnt exist*/, or tabControl.Items /*only TabItems*

如何(从TabControl)获取嵌套在Tabitem中的所有控件/UI元素

我什么都试过了,但没能得到

(设置所选选项卡):

现在我需要这样的东西:

  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);

TabControl具有Items属性(派生自ItemsControl),该属性返回所有TabItems-。或者,您可以遍历可视化树:

var firstStackPanelInTabControl = FindVisualChildren<StackPanel>(tabControl).First();
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;
    }
  }
}
公共静态IEnumerable FindVisualChildren(DependencyObject根对象),其中T:DependencyObject
{
if(rootObject!=null)
{
for(int i=0;i
这种方法可能会帮助您:

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;
      }
    }
  }
}
公共静态IEnumerable FindChildren(此DependencyObject源)
其中T:DependencyObject
{
如果(源!=null)
{
var childs=GetChildObjects(源);
foreach(子对象中的DependencyObject子对象)
{
//分析子项是否与请求的类型匹配
if(child!=null&&child为T)
{
收益率(T)子项;
}
//递归树
foreach(FindChildren(child))中的T后代
{
产量回报后代;
}
}
}
}

请参阅全文(在WPF树中查找元素)。

对于我来说,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;
    }
公共静态列表obtenerControls(DependencyObject父对象)
其中T:DependencyObject
{
列表结果=新列表();
如果(父项!=null)
{
foreach(LogicalTreeHelper.GetChildren(父级)中的变量子级)
{                   
var childType=child作为T;
if(childType!=null)
{
结果:添加((T)子项);
}
foreach(obtenerControls中的其他变量(子对象作为DependencyObject))
{
结果:添加(其他);
}
}
}
返回结果;
}

我不需要TabItems,我需要TabItem本身的控件/UIElements,它嵌套在TabItem或TabControl中,然后您可以始终遍历可视化树。我在答案中添加了示例代码,这是一个很好的答案。你能详细说明一下T:DependencyObject的作用吗?或者我可以在其中找到更多信息的链接?“其中T:DependencyObject”是一个泛型类型约束()。基本上,它只是限制类型T必须从DependencyObject派生,因此在方法体中我可以使用VisualTreeHelper.GetChild,它需要DependencyObject作为参数。tabitem可能需要在呈现其子对象之前首先触发UpdateLayout。每个示例都会在选择/打开时发生这种情况。呈现所选选项卡的布局后,您将需要该选项卡及其子级。已编辑的答案。。它仍然始终是1(网格)
 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;
    }