My VisualTreeHelper search the visual tree

1 03 2010

VisualTreeHelper class is very useful so far i found.

The VisualTreeHelper type exposes the foure members.

Check here:

http://msdn.microsoft.com/en-us/library/system.windows.media.visualtreehelper_members%28VS.95%29.aspx

We could use VisualTreeHelper and write another two method which i found very useful like below:

SearchFrameworkElement – Get the child FrameworkElement with a given name from the visual tree of a parent FrameworkElement.

GetAllChildFrameworkElement – Get All Child FrameworkElement of given FrameworkElement

Source Code below:

public class MyVisualTreeHelper
{
    /// <summary>
    /// Get the child FrameworkElement with a given name
    /// from the visual tree of a parent FrameworkElement.
    /// </summary>
    /// <param name="parentFrameworkElement">Parent FrameworkElement</param>
    /// <param name="childFrameworkElementNameToSearch">Child FrameworkElement name</param>
    /// <returns>Child FrameworkElement with a given name</returns>
    public static FrameworkElement SearchFrameworkElement(FrameworkElement parentFrameworkElement, string childFrameworkElementNameToSearch)
    {
        FrameworkElement childFrameworkElementFound = null;
        SearchFrameworkElement(parentFrameworkElement, ref childFrameworkElementFound, childFrameworkElementNameToSearch);
        return childFrameworkElementFound;
    }
    /// <summary>
    /// Get All Child FrameworkElement of given FrameworkElement
    ///</summary>
    /// <param name="parentElement">Parent FrameworkElement whose child FrameworkElement's will be searched</param>
    /// <returns>List of Child FrameworkElement</returns>
    public static List<FrameworkElement> GetAllChildFrameworkElement(FrameworkElement parentElement)
    {
        List<FrameworkElement> childFrameworkElementFound = new List<FrameworkElement>();
        SearchAllChildFrameworkElement(parentElement, childFrameworkElementFound);
        return childFrameworkElementFound;
    }
    //SearchFrameworkElement helper
    private static void SearchFrameworkElement(FrameworkElement parentFrameworkElement, ref FrameworkElement childFrameworkElementToFind, string childFrameworkElementName)
    {
        int childrenCount = VisualTreeHelper.GetChildrenCount(parentFrameworkElement);
        if (childrenCount > 0)
        {
            FrameworkElement childFrameworkElement = null;
            for (int i = 0; i < childrenCount; i++)
            {
                childFrameworkElement = (FrameworkElement)VisualTreeHelper.GetChild(parentFrameworkElement, i);
                if (childFrameworkElement != null && childFrameworkElement.Name.Equals(childFrameworkElementName))
                {
                    childFrameworkElementToFind = childFrameworkElement;
                    return;
                }
                SearchFrameworkElement(childFrameworkElement, ref childFrameworkElementToFind, childFrameworkElementName);
            }
        }
    }
    //GetAllChildFrameworkElement helper
    private static void SearchAllChildFrameworkElement(FrameworkElement parentFrameworkElement, List<FrameworkElement> allChildFrameworkElement)
    {
        int childrenCount = VisualTreeHelper.GetChildrenCount(parentFrameworkElement);
        if (childrenCount > 0)
        {
            for (int i = 0; i < childrenCount; i++)
            {
                FrameworkElement childFrameworkElement = (FrameworkElement)VisualTreeHelper.GetChild(parentFrameworkElement, i);
                allChildFrameworkElement.Add(childFrameworkElement);
                SearchAllChildFrameworkElement(childFrameworkElement, allChildFrameworkElement);
            }
        }
    }
}
Advertisements

Actions

Information

One response

15 08 2010
83. Dynamic Left Click Context Menu in Silverlight « Sharker Khaleed Mahmud Silverlight Tips & Tricks

[…] will use Amyo’s visual tree helper to find all the menu […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: