I have construction:
Grid a = ((((usercontrol.Parent as DockPanel).Parent as ScrollViewer).Parent as Grid)
Is it possible to find a tree or a parent element?
example: Grid a = GetFirstParent(usercontrol,"Grid") Grid - is Type element
I have construction:
Grid a = ((((usercontrol.Parent as DockPanel).Parent as ScrollViewer).Parent as Grid)
Is it possible to find a tree or a parent element?
example: Grid a = GetFirstParent(usercontrol,"Grid") Grid - is Type element
http://www.scottlogic.co.uk/blog/colin/2010/03/linq-to-visual-tree/ Take a look at this link, I think it might help.
Use the VisualTreeHelper class.
It has a method, GetParent, that returns the parent of a control (a DependencyObject really).
Grid a = userControl.FindParent<Grid>();
public static T FindParent<T>(this DependencyObject startElement)
where T : DependencyObject
{
DependencyObject parent = GetParentObject(startElement);
if (parent == null)
return null;
T typedParent = parent as T;
if (typedParent != null)
{
return typedParent;
}
return FindParent<T>(parent);
}