如何在视觉树中查找元素?WP7 [英] How to find element in visual tree? wp7

查看:22
本文介绍了如何在视觉树中查找元素?WP7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How I can find element that contains in App.xaml, grid with name "audioPanel"? I tried:

Grid found = this.FindChild(^*我找不到合适的东西*^, "audioPanel");

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

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

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

推荐答案

更新:您需要结合我的答案和 H.B. 的答案.使用下面的 FindChild 版本,把你对 FindChild 的调用改成这样

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");

由于您正在设计电话应用程序框架的样式,因此 H.B. 评论中的应用它的控件"很可能是 RootVisual(可能有例外,我不确定).

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).

另外,我假设 pastebin 中 App.xaml 的..."部分在某处有一个 ContentPresenter,否则我认为你的风格不会起作用.

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.

结束更新

如果您在链接到的问题中使用已接受的答案(WPF 查找方法控件)并且您的audioPanel"网格嵌套在另一个网格中,那么您仍然找不到它 - 该代码中有错误.这是一个即使嵌套控件也能工作的更新版本:

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

这篇关于如何在视觉树中查找元素?WP7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆