如何在可视树中找到元素? WP7

时间:2011-08-12 00:36:44

标签: c# wpf silverlight windows-phone-7

如何在App.xaml中找到包含名称为“audioPanel”的网格的元素? 我试过了:

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 个答案:

答案 0 :(得分:2)

更新:您需要结合我的回答和H.B.的回答。使用下面的FindChild版本,并将您对FindChild的调用更改为

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

由于您正在为手机应用程序框架设置样式,因此H.B.评论中的“应用它的控件”很可能是RootVisual(可能有例外,我不确定)。

另外,我假设你的app.xaml在pastebin中的“...”部分在那里有一个ContentPresenter,否则我认为你的风格不会起作用。

END UPDATE

如果您在链接到(WPF ways to find controls)的问题中使用了接受的答案,并且您的'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'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;
    }
}

答案 1 :(得分:1)

如果它在App.xaml中,我会认为它是Application.Resources中资源的一部分,因为任何地方都没有使用的资源不在可视化树中,这是行不通的。

如果这是真的,你可以尝试从资源中获取对象的根,然后从那里搜索,例如。

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

答案 2 :(得分:0)

为了完整性,E. Z. Hart的版本有一个错误,因为找到的子子被覆盖。这是一个工作版本

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