I'm trying to get a list of all controls of a given type on a given Page, but I'm encountering problems. It seems that it's possible that the VisualTreeHelper only returns controls that have been loaded? I tried turning off Virtualization, but that didn't seem to help. Can anyone think of another way to get all the control or perhaps force a load of the UI so that the following method does work?
I borrowed this from MSDN:
public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
{
if (depObj != null)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
if (child != null && child is T)
{
yield return (T)child;
}
foreach (T childOfChild in FindVisualChildren<T>(child))
{
yield return childOfChild;
}
}
}
}