How to find element in visual tree? wp7

前端 未结 3 1983
[愿得一人]
[愿得一人] 2020-12-21 07:20

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

Grid found = this.FindChild(^*I can\'t find anything suit

3条回答
  •  礼貌的吻别
    2020-12-21 08:03

    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(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(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(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(child, childName);
                }
                else
                {
                    // child element found.
                    foundChild = (T) child;
                    break;
                }
            }
    
            return foundChild;
        }
    }
    

提交回复
热议问题