Saturday 24 May 2014

Helper class to find the Parent of a Dependency object in WPF

In this article we are going to see how to get the parent of a Dependency object with specific type or General. For that we need to write a Helper class which will result in the find the data in the visual tree helper.

  static class UIHelper
    {
      
        public static T TryFindParent<T>(this DependencyObject child)
            where T : DependencyObject
        {           
            DependencyObject parentObject = GetParentObject(child);
           
            if (parentObject == null)
                return null;
           
            T parent = parentObject as T;
            if (parent != null)
            {
                return parent;
            }
            else
            {               
                return TryFindParent<T>(parentObject);
            }
        }
         
        public static DependencyObject GetParentObject(this DependencyObject child)
        {
            if (child == null) return null;
           
            ContentElement contentElement = child as ContentElement;
            if (contentElement != null)
            {
                DependencyObject parent = ContentOperations.GetParent(contentElement);
                if (parent != null) return parent;

                FrameworkContentElement framecontent = contentElement as                                                                             FrameworkContentElement;
                return framecontent != null ? framecontent.Parent : null;
            }
           
            FrameworkElement frameworkElement = child as FrameworkElement;
            if (frameworkElement != null)
            {
                DependencyObject parent = frameworkElement.Parent;
                if (parent != null) return parent;
            }
           
            return VisualTreeHelper.GetParent(child);
        }



    } 

From the above class we have two methods one which get the input of return type of parent another one just get the parent object of a dependency child object.

Syntax for using the above code


private static void OnZindexingChanged(DependencyObject d,                                                                    DependencyPropertyChangedEventArgs e)
        {
          OrderingPanel pan = UIHelper.TryFindParent<OrderingPanel (d);                      

        } 


Let we explain how it is working When TryFindParent called it get the dependency object then we are trying to get the parent of that object, in that we are split the code in to three categories

First we cast the child object in to the ContentElement then if it is not null, then parent object must be from the ContentOperations or From FrameworkContentElement

in the another thing we are casting the child as Framework element then get the value from the parent property, at-last tried in the VisualTreeHelper.GetParent

Base on the above three we get the parent object as DependencyObject then checking for the matched parent type if not found then again iterate with that parent object up to get the correct required type or return null.

I hope form this article you can learn how to get the parent of a dependency object with particular type.

No comments:

Post a Comment