Skip to content

Instantly share code, notes, and snippets.

@5cover
Created May 1, 2023 16:57
Show Gist options
  • Save 5cover/9506677440a783f30f1933724c609f4b to your computer and use it in GitHub Desktop.
Save 5cover/9506677440a783f30f1933724c609f4b to your computer and use it in GitHub Desktop.
FindVisualChildren C# WPF
public static IEnumerable<T> FindVisualChildren<T>(this DependencyObject depObj) where T : DependencyObject
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
var child = VisualTreeHelper.GetChild(depObj, i);
if (child is T t)
{
yield return t;
}
foreach (T childOfChild in FindVisualChildren<T>(child))
{
yield return childOfChild;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment