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