VisualTreeHelper Class in wpf
One nice use of VisualTreeHelper class
VisualTreeHelper class provides utility members that provide common tasks regarding nodes in the visual tree like finding childs, parents,sbilings etc. This class cannot be created directly in xaml.
Following example code shows you how to find the first child object of a certain type for eg: Find the ScrollViewer visual node on the listbox controls
C# code
—————————–
public static TItem FindVisualChild<TItem>(DependencyObject obj) where TItem : DependencyObject
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
var child = VisualTreeHelper.GetChild(obj, i);
if (child != null && child is TItem)
return (TItem)child;
var childOfChild = FindVisualChild(child);
if (childOfChild != null)
{
return childOfChild;
}
}
return null;
}
Use this class like this:
ScrollViewer scrollViewer = VisualHelper.FindVisualChild<ScrollViewer>(listbox);






