Facebook Twitter Gplus LinkedIn RSS
formats

Silverlight AllChildren: find all of the visual children of a FrameworkElement

Our application has some occasions where we need to iterate the entire child tree of a visual component. Not a common thing in many applications, I use it to pass visual states to the sub elements of a tree, but if you need a routine for this purpose then I thought I would post mine here:  


        public static List<T> AllChildren<T>(this FrameworkElement ele, Func<DependencyObject, bool> whereFunc = null) where T : class
        {
            if (ele == null)
                return null;
            var output = new List<T>();
            var c = VisualTreeHelper.GetChildrenCount(ele);
            for (var i = 0; i < c; i++)
            {
                var ch = VisualTreeHelper.GetChild(ele, i);
                if (whereFunc != null)
                {
                    if (!whereFunc(ch))
                    {
                        continue;
                    }
                }
                if ((ch is T))
                    output.Add(ch as T);
                if (!(ch is FrameworkElement))
                    continue;

                output.AddRange((ch as FrameworkElement).AllChildren<T>(whereFunc));
            }
            return output;
        }

The function is an extension method that uses the generic Type to decide on the types of children to return.  It takes an optional “Where function” that can be used to stop iterating down branches of the visual tree – please note that the Where function doesn’t only get passed the Type components, it gets everything so you can stop the recursive operation when you want to.  If you don’t pass the Where parameter then all visual children are returned.  

 

                foreach (var c in panel.AllChildren<Control>((child) => !(child is FdTreeViewItem)))
                {
                    VisualStateManager.GoToState(c, "FlowSelected", true);
                } 

   

5 Responses

  1. shabbir

    excellent code sir ,thanks for sharing

  2. avinash

    Does anyone know how can I find Logical childs in a usercontrol though they r not visible ?

    • FirstVisualChild etc should work so long as the children are connected, it doesn’t require that they be visible – just that the relationship exists. Not sure why you are having a problem, can you post a larger example?

  3. avinash

    Because it is Visual Tree.. Only visible child ontrols are accessible. I think we need to user Logical Tree..Kinda.. I m also facing same issue.

  4. Misc

    Thanks for the code.
    I have written a similar routine for myself, and ran into a problem with it. So I was looking online to see if anyone else had the same problem. I found your code,.. and also found the same problem in it.

    If I have a UserControl with controls on it and a TabControl also on it, with many TabItems in it, your code can return any Control on the UserControl and any control from the FIRST TabItem only. It stops there. It does not find and return any control from the second TabItem onwards.

    Thanks.

Home Uncategorized Silverlight AllChildren: find all of the visual children of a FrameworkElement