I have a ListBox with an ItemsPanel
Try out following extension method:
var childStackPanels = FindVisualChildren(ThumbListBox);
Method itself:
public static IEnumerable FindVisualChildren(this DependencyObject depObj) where T : DependencyObject
{
if (depObj != null)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
var typedChild = child as T;
if (typedChild != null)
{
yield return typedChild;
}
foreach (T childOfChild in FindVisualChildren(child))
{
yield return childOfChild;
}
}
}
}
PS: You can yourself extend it to check for specific control name so method would return single control instead of list.