I need to search a WPF control hierarchy for controls that match a given name or type. How can I do this?
If you want to find ALL controls of a specific type, you might be interested in this snippet too
public static IEnumerable FindVisualChildren(DependencyObject parent)
where T : DependencyObject
{
int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < childrenCount; i++)
{
var child = VisualTreeHelper.GetChild(parent, i);
var childType = child as T;
if (childType != null)
{
yield return (T)child;
}
foreach (var other in FindVisualChildren(child))
{
yield return other;
}
}
}