Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
826 views
in Technique[技术] by (71.8m points)

c# - How to find element in visual tree? wp7

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...