How to find element in visual tree? wp7

2019-01-28 08:32发布

问题:

How I can find element that contains in App.xaml, grid with name "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

回答1:

UPDATE: You need a combination of both my answer and H.B.'s 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're styling the phone application frame, the "control on which it is applied" from H.B.'s comment is pretty likely to be the RootVisual (there may be exceptions to this, I'm not sure).

Also, I'm assuming that the "..." parts of your App.xaml in pastebin have a ContentPresenter in there somewhere, otherwise I don't think your style will work.

END UPDATE

If you're using the accepted answer in the question you linked to (WPF ways to find controls) and your 'audioPanel' grid is nested inside of another grid, then you still won't find it - there's an error in that code. Here's an updated version that will work even if the control is nested:

    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's name is set for search
                if (frameworkElement != null && frameworkElement.Name == childName)
                {
                    // if the child's 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:

If it is in App.xaml i would assume it to be part of a resource in Application.Resources, as resources that are not used anywhere are not in the visual tree this won't do.

If this is true you can try getting the root of the object from the resources and search from there, e.g.

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


回答3:

just for completeness, the version of E. Z. Hart has a bug, as found sub childs are overwritten. here is a 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's name is set for search
                if (frameworkElement != null && frameworkElement.Name == childName)
                {
                    // if the child's 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;
    }