Skip to content

Instantly share code, notes, and snippets.

@mvacha
Created September 21, 2016 13:00
Show Gist options
  • Save mvacha/39ae973daf367ba4809a756b3e81f606 to your computer and use it in GitHub Desktop.
Save mvacha/39ae973daf367ba4809a756b3e81f606 to your computer and use it in GitHub Desktop.
public static T GetVisualChildByType<T>(this DependencyObject referenceVisual) where T : DependencyObject
{
DependencyObject child = null;
for (Int32 i = 0; i < VisualTreeHelper.GetChildrenCount(referenceVisual); i++)
{
child = VisualTreeHelper.GetChild(referenceVisual, i) as DependencyObject;
if (child != null && child is T)
{
break;
}
else if (child != null)
{
child = GetVisualChildByType<T>(child);
if (child != null && child is T)
{
break;
}
}
}
return child as T;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment