How do I loop through the all controls in a window in WPF?
I've used the following to get all controls.
public static IList GetControls(this DependencyObject parent)
{
var result = new List();
for (int x = 0; x < VisualTreeHelper.GetChildrenCount(parent); x++)
{
DependencyObject child = VisualTreeHelper.GetChild(parent, x);
var instance = child as Control;
if (null != instance)
result.Add(instance);
result.AddRange(child.GetControls());
}
return result;
}