Code Reference A collection of code for my reference (and perhaps other people too)

20Aug/090

Find a child UIElement

Here is a nice little method that will find the first occurance of child UIElement.

XAML can get long and complex. This helped me find specific elements so I can set focus on them or do some other type of operation.

Call the method like this

UIElement result = FocusOnFirstElement(myLayoutRoot, typeof(Expander));

Here is the method:

/// <summary>
/// Find the first UIElement of a specific type within the specified root element.
/// UIElements in WPF have child elements under one of the following properties: Content, Child, Items, Children
/// This loops thru itself looking for any child elements.
/// </summary>
/// <param name="rootElement"></param>
/// <param name="typeToFind"></param>
/// <returns>UIElement or null if the element was not found</returns>
public UIElement FocusOnFirstElement(UIElement rootElement, Type typeToFind)
{
    bool found = false;
    UIElement currentElement = rootElement;

    var x1 = currentElement.GetType().GetProperty("Content");
    if (x1 != null)
    {
        var content = currentElement.GetType().GetProperty("Content").GetValue(currentElement, BindingFlags.GetProperty, null, null, null);

        if (content == null)
            return null;

        if (content.GetType() == typeToFind)
            return content as UIElement;

        return FocusOnFirstElement(content as UIElement, typeToFind);
    }

    var x2 = currentElement.GetType().GetProperty("Child");
    if (x2 != null)
    {
        var child = currentElement.GetType().GetProperty("Child").GetValue(currentElement, BindingFlags.GetProperty, null, null, null);

        if (child == null)
            return null;

        if (child.GetType() == typeToFind)
            return child as UIElement;

        return FocusOnFirstElement(child as UIElement, typeToFind);
    }

    var x4 = currentElement.GetType().GetProperty("Items");
    if (x4 != null)
    {
        ItemCollection items = (ItemCollection)currentElement.GetType().GetProperty("Items").GetValue(currentElement, BindingFlags.GetProperty, null, null, null);

        if (items == null)
            return null;

        foreach (var item in items)
        {
            if (item.GetType() == typeToFind)
                return item as UIElement;

            UIElement retunedElement = FocusOnFirstElement(item as UIElement, typeToFind);
            if (retunedElement != null)
                return retunedElement;
        }
    }

    var x5 = currentElement.GetType().GetProperty("Children");
    if (x5 != null)
    {
        UIElementCollection children = (UIElementCollection)currentElement.GetType().GetProperty("Children").GetValue(currentElement, BindingFlags.GetProperty, null, null, null);

        if (children == null)
            return null;

        foreach (var item in children)
        {
            if (item.GetType() == typeToFind)
                return item as UIElement;

            UIElement retunedElement = FocusOnFirstElement(item as UIElement, typeToFind);
            if (retunedElement != null)
                return retunedElement;
        }
    }

    return null;
}
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

No trackbacks yet.