tags:

views:

22

answers:

3

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

+1  A: 

http://www.scottlogic.co.uk/blog/colin/2010/03/linq-to-visual-tree/ Take a look at this link, I think it might help.

Jeppe Vammen Kristensen
A: 

Use the VisualTreeHelper class.

It has a method, GetParent, that returns the parent of a control (a DependencyObject really).

Rune Grimstad
A: 
    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);
    }
John Bowen