In many scenarios we will require to get to the inner elements of our UserControl. The first option is to define x:Name attributes in XAML or use the FindName. But in scenarios where the hierarchy is created dynamically either through code or databinding this wont work. The VisualTreeHelper will allow us to get to the Childrens of an element.
A common scenarios is how to get to the rendered elements of a ListBox. Since Silverlight is datacentric instead of control centric, the Items collection will return the actual DataSource and not the controls as the GridView would in Web. For most scenarios databinding would do the trick, but imagine if you need to change the color of a TextBox inside a ListBoxItem or if you have nested ListBoxes and you need to iterate all the ListBoxes and get the value of their InnerListBoxe's SelectedItem. Well for this and other scenarios you can use VisualTreeHelper its a static class with many straightforward methods that receive a depencency object.
public static class VisualTreeHelper
public static DependencyObject GetChild(DependencyObject reference, int childIndex);
public static int GetChildrenCount(DependencyObject reference);
public static DependencyObject GetParent(DependencyObject reference);
Tomorrow I will talk about how I created a simpler API using extension method and a bit of LINQ.