Get and play through controls from TabItem?

How do I get all the controls / UIElements that are nested in a Tabitem (from a TabControl)?

I've tried everything but couldn't get them.

(Set the SelectedTab value):

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

      

Now I need something like this:

  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
        }
  }

      

After Silvermind: By doing this, the graph is always 1:

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

      

+3


source to share


3 answers


TabControl has an Items property (derived from ItemsControl) which returns all TabItems - http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.items.aspx . Or, you can navigate the visual tree:

var firstStackPanelInTabControl = FindVisualChildren<StackPanel>(tabControl).First();

      



Using:

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

      

+3


source


Maybe a method like this will help you:

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

      



See the full article (Finding Items in the WPF Tree) here .

+2


source


for me VisualTreeHelper.GetChildrenCount always returns 0 for tab control, I had to use this method instead

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

      

0


source







All Articles