The following snippet provides a generic method to walk up the visual tree of Silverlight in order to find an element of a given type. It will return the first found item of said type, or null if the search ends at the visual tree root without any results.
public static T FindUpVisualTree<T>(DependencyObject initial) where T : DependencyObject
DependencyObject current = initial;
while (current != null && current.GetType() != typeof(T))
current = VisualTreeHelper.GetParent(current);
return current as T;
Find the first
Grid containing the
x:Name="button01"), regardless if the
Button is located directly in a
Grid or nested within some other containers.
Grid gridContainingButton = FindUpVisualTree<Grid>(button01);
Understanding the Visual
Tree and Logical Tree in WPF by Josh Smith