What would be the elegant solution for getting UserControl, based on its child Visual
At the moment I do a loop:
while (canduate.Parent != null)
{
if (candidate.GetType().IsAssignableFrom(typeof(MyDesirableType)) return candidate;
else candidate = ((FrameworkElement)candidate).Parent;
}
which doesn't seem to be an elegant solution.

Visual -> Control (UserControl)
ArcyQwerty
Something like this would probably be the most resilient way of doing it:
public static T FindNearestAncestor<T>(DependencyObject dependencyObject) where T : DependencyObject{
T result = null;
while(dependencyObject != null
&&
result == null)
{
dependencyObject = VisualTreeHelper.GetParent(dependencyObject);
result = dependencyObject as T;
} return result;
}
Used like so:
MyControl result = FindNearestAncestor<MyControl>(someVisual);