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

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

问题描述

我如何才能找到一个包含App.xaml中的元素,并网名称为audioPanel?
我想:

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

电网发现= this.FindChild<电网>(^ *我找不到合适的* ^什么, audioPanel);

WPF的方式找到控制

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

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

推荐答案

更新:你需要的组合我的两个答案,HB的回答。使用下面FindChild的版本,并更改​​来电FindChild看起来像

UPDATE: You need a combination of both my answer and H.B.'s answer. Use the version of FindChild below, and change you call to FindChild to look like

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



既然你造型手机应用程序框架中,控制其所适用的从HB的评论很可能是RootVisu​​al(有可能是例外,我不知道)。

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

另外,我假设的...在你的引擎收录的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.

END更新

如果您使用的是您链接到(的 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天全站免登陆