12

I would like to find all of the controls within a WPF control. I have had a look at a lot of samples and it seems that they all either require a Name to be passed as parameter or simply do not work.

I have existing code but it isn't working properly:

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

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

For instance it will not get a DataGrid within a TabItem.

Any suggestions?

Chrisjan Lodewyks
  • 1,162
  • 4
  • 13
  • 25

2 Answers2

19

You can use these.

 public static List<T> GetLogicalChildCollection<T>(this DependencyObject parent) where T : DependencyObject
        {
            List<T> logicalCollection = new List<T>();
            GetLogicalChildCollection(parent, logicalCollection);
            return logicalCollection;
        }

 private static void GetLogicalChildCollection<T>(DependencyObject parent, List<T> logicalCollection) where T : DependencyObject
        {
            IEnumerable children = LogicalTreeHelper.GetChildren(parent);
            foreach (object child in children)
            {
                if (child is DependencyObject)
                {
                    DependencyObject depChild = child as DependencyObject;
                    if (child is T)
                    {
                        logicalCollection.Add(child as T);
                    }
                    GetLogicalChildCollection(depChild, logicalCollection);
                }
            }
        }

You can get child button controls in RootGrid f.e like that:

 List<Button> button = RootGrid.GetLogicalChildCollection<Button>();
aydjay
  • 858
  • 11
  • 25
Farhad Jabiyev
  • 26,014
  • 8
  • 72
  • 98
  • 8
    Logical tree does not contain visuals from control's template. Your code can't find *all* child controls by definition. – Dennis Feb 14 '13 at 13:24
  • 1
    Thanx that worked! It gets my `DataGrid ` unlike my own code! – Chrisjan Lodewyks Feb 14 '13 at 13:26
  • 1
    @ChrisjanLodewyks Glad to hear it. – Farhad Jabiyev Feb 14 '13 at 13:32
  • LogicalTreeHelper.GetChildren() was the key for me here. – ouflak Mar 25 '14 at 18:05
  • I've searched all over the web for a solution like this. I tried many things to make this work but everything failed. Till i found this post, everything is working now. THANK YOU! – NickGames Jan 15 '15 at 14:11
  • Should be noted that the IEnumerable in this code is taken from System.Collections, not from System.Collections.Generic ([reference](https://stackoverflow.com/a/17703382/997940)) – Yoav Feuerstein Jul 31 '17 at 08:43
  • You should add a level/rank info, if the Canvas contains another Canvas? Both with some buttons inside.You will get all the buttons at the same level, and this is not the reality. Did I miss something ? – pix Mar 09 '20 at 12:50
-1

You can use this Example:

public Void HideAllControl()
{ 
           /// casting the content into panel
           Panel mainContainer = (Panel)this.Content;
           /// GetAll UIElement
           UIElementCollection element = mainContainer.Children;
           /// casting the UIElementCollection into List
           List < FrameworkElement> lstElement =    element.Cast<FrameworkElement().ToList();

           /// Geting all Control from list
           var lstControl = lstElement.OfType<Control>();
           foreach (Control contol in lstControl)
           {
               ///Hide all Controls 
               contol.Visibility = System.Windows.Visibility.Hidden;
           }
}
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364