Get Children for DependencyObject : DependencyProperty « Windows Presentation Foundation « C# / C Sharp






Get Children for DependencyObject

        

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media;

static class common
{
    public static List<UIElement> GetChildren(this DependencyObject parent, Type targetType)
    {
        List<UIElement> elements = new List<UIElement>();

        int count = VisualTreeHelper.GetChildrenCount(parent);
        if (count > 0)
        {
            for (int i = 0; i < count; i++)
            {
                UIElement child = (UIElement)VisualTreeHelper.GetChild(parent, i);
                if (child.GetType() == targetType || targetType.IsAssignableFrom(child.GetType()))
                {
                    elements.Add(child);
                }
                elements.AddRange(GetChildren(child, targetType));
            }
        }
        return elements;
    }
}

   
    
    
    
    
    
    
    
  








Related examples in the same category

1.DependencyProperty.RegisterReadOnly to create read only Dependency PropertyDependencyProperty.RegisterReadOnly to create read only Dependency Property
2.Clear locally set values and restore the default values of dependency propertiesClear locally set values and restore the default values of dependency properties
3.Find children
4.Find Control With Tag