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

本文关键字:元素 wp7 可视化 | 更新日期: 2023-09-27 18:05:25

我怎么能找到元素,包含在App.xaml,网格名称"audioPanel"?我试着:

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

如何按名称或类型查找WPF控件?

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

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

更新:你需要我的答案和H.B.的答案的组合的答案。使用下面的FindChild版本,并更改对FindChild的调用,看起来像

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

既然你正在样式化电话应用程序框架,"它被应用的控件"来自hb的评论很可能是RootVisual(可能有例外,我不确定)。

另外,我假设你的App.xaml在pastebin的"…"部分有一个ContentPresenter在那里的某个地方,否则我不认为你的风格将工作。

更新结束

如果你在你链接到的问题中使用公认的答案(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'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;
    }
}

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

如果这是真的,你可以尝试从资源中获取对象的根目录,并从那里开始搜索,例如

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

只是为了完整起见,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;
    }