How to find an item in a visual tree? WP7

How can I find the element contained in App.xaml, a grid named "audioPanel"? I tried:

Grid found = this.FindChild<Grid>(^*I can't find anything suitable*^, "audioPanel");

How can I find WPF controls by name or type?

UPD: App.xaml http://pastebin.com/KfWbjMV8

+3
source share
3 answers

UPDATE: you need a combination of both my answer and the HB answer. Use the version of FindChild below and change your call to FindChild to look like

var grid = FindChild<Grid>(Application.Current.RootVisual, "audioPanel");

Since you are setting up the application frame for the phone, the “control on which it is applied” from HB will most likely be RootVisual (there may be exceptions to this, I'm not sure).

, , "..." App.xaml pastebin ContentPresenter - , , .

END UPDATE

, (WPF ), audioPanel , - . , , :

    public static T FindChild<T>(DependencyObject parent, string childName)
        where T : DependencyObject
    {
        // Confirm parent and childName are valid. 
        if (parent == null)
        {
            return null;
        }

        T foundChild = null;

        int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
        for (int i = 0; i < childrenCount; i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(parent, i);
            // If the child is not of the request child type child
            var childType = child as T;
            if (childType == null)
            {
                // recursively drill down the tree
                foundChild = FindChild<T>(child, childName);

                // If the child is found, break so we do not overwrite the found child. 
                if (foundChild != null)
                {
                    break;
                }
            }
            else if (!string.IsNullOrEmpty(childName))
            {
                var frameworkElement = child as FrameworkElement;
                // If the child name is set for search
                if (frameworkElement != null && frameworkElement.Name == childName)
                {
                    // if the child name is of the request name
                    foundChild = (T) child;
                    break;
                }

                // Need this in case the element we want is nested
                // in another element of the same type
                foundChild = FindChild<T>(child, childName);
            }
            else
            {
                // child element found.
                foundChild = (T) child;
                break;
            }
        }

        return foundChild;
    }
}
+2

App.xaml, , Application.Resources, , , , .

, , .

var root = Application.Current.Resources["MyKey"] as FrameworkElement;
Grid found = this.FindChild<Grid>(root, "audioPanel");
+1

just for completeness, the EZ Hart version has an error, since the found child units are overwritten. here is the working version

public static T FindChild<T>(this DependencyObject parent, string childName = null) where T : DependencyObject
    {
        // Confirm parent and childName are valid. 
        if (parent == null)
            return null;

        T foundChild = null;

        var childrenCount = VisualTreeHelper.GetChildrenCount(parent);
        for (var i = 0; foundChild == null && i < childrenCount; i++)
        {
            var child = VisualTreeHelper.GetChild(parent, i);                

            // If the child is not of the request child type child
            var childType = child as T;
            if (childType == null)
            {                   
                // recursively drill down the tree
                foundChild = FindChild<T>(child, childName);                    
            }
            else if (!string.IsNullOrEmpty(childName))
            {
                var frameworkElement = child as FrameworkElement;
                // If the child name is set for search
                if (frameworkElement != null && frameworkElement.Name == childName)
                {
                    // if the child name is of the request name
                    foundChild = (T)child;                        
                }
                else
                {
                    // Need this in case the element we want is nested
                    // in another element of the same type
                    foundChild = FindChild<T>(child, childName);
                }                    
            }
            else
            {
                // child element found.
                foundChild = (T)child;                    
            }
        }

        return foundChild;
    }
0
source

All Articles