简体   繁体   English

VisualTreeHelper找不到DependencyObject的子代,如何可靠地找到对象?

[英]VisualTreeHelper not finding children of DependencyObject, How can I reliably find objects?

I have a UserControl called ZoneContainer . 我有一个名为ZoneContainer的UserControl。 This has a property that which contains a ListBox containing a number of ListItem s. 它具有一个属性,该属性包含一个包含多个ListItemListBox Each ListItem contains a DockPanel . 每个ListItem包含一个DockPanel

I'm trying to use a the following code to find the children that exist inside ZoneContainer but childrenCount is 0 every time. 我正在尝试使用以下代码查找ZoneContainer内部存在的子代,但是childrenCount每次都是0。

var parent = this as DependencyObject; // I can see that this is populated.

int childrenCount = VisualTreeHelper.GetChildrenCount(parent);

Is there another way to find a specific child object inside a list of objects? 还有另一种方法可以在对象列表中找到特定的子对象吗? Ultimately I'm trying to find the DockPanel, but it's not finding any children even though I know they're in the object. 最终,我试图找到DockPanel,但是即使我知道他们在对象中,也没有找到任何孩子。

The basic problem here is that not all childs are part of the VisualTree 这里的基本问题是,并非所有孩子都属于VisualTree。
You can find more information about this problem in this articel from Josh Smith 您可以在此找到有关此问题的详细信息articel从约什-史密斯

here are my extension to get all Childs 这是我得到所有孩子的扩展

    public static IEnumerable<DependencyObject> getChilds(this DependencyObject parent)
    {
        if (parent == null) yield break;

        //use the logical tree for content / framework elements
        foreach (object obj in LogicalTreeHelper.GetChildren(parent))
        {
            var depObj = obj as DependencyObject;
            if (depObj != null)
                yield return depObj;
        }

        //use the visual tree for Visual / Visual3D elements
        if (parent is Visual || parent is Visual3D)
        {
            int count = VisualTreeHelper.GetChildrenCount(parent);
            for (int i = 0; i < count; i++)
            {
                yield return VisualTreeHelper.GetChild(parent, i);
            }
        }
    }

This is the function I've got lurking in my library. 这是我潜藏在库中的功能。 I've never had any trouble with it, but it does have a GetChildrenCount() call in it so if that's not working for you you may have a bigger problem. 我从来没有遇到过任何麻烦,但是它确实有一个GetChildrenCount()调用,因此,如果这对您不起作用,那么您可能会遇到更大的问题。

Public Shared Function FindVisualChild(Of T As DependencyObject)(ByVal element As DependencyObject) As T
    If element Is Nothing Then
        Return Nothing
    ElseIf TypeOf (element) Is T Then
        Return element
    Else
        Dim count = VisualTreeHelper.GetChildrenCount(element)
        For index As Integer = 0 To count - 1
            Dim child As DependencyObject = VisualTreeHelper.GetChild(element, index)
            If TypeOf (child) Is T Then
                Return child
            Else
                Dim grandchild As T = FindVisualChild(Of T)(child)
                If grandchild IsNot Nothing Then Return grandchild
            End If
        Next
    End If

    Return Nothing
End Function

Usage: x = FindVisualChild(Of DockPanel)(ParentObject) 用法:x = FindVisualChild(DockPanel)(ParentObject)

Yes, I know it's VB. 是的,我知道这是VB。 It's about time one of you C# guys had to convert code for once! 现在是时候让你们C#伙计们不得不一次转换代码了! :) :)

I resolved this issue by querying the objects rather than crawling the visual tree. 我通过查询对象而不是对可视树进行爬网来解决此问题。

var header = container.ListBox.Items.Cast<ListBoxItem>()
    .Select(item => (MyType) item.Content)
    .FirstOrDefault(myType => myType.dpHeader.Name == "whatever").dpHeader;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在VisualTreeHelper.GetParent()更改时将属性附加到控件或使用会更新的绑定? - How can I attach a property to a control or use a binding that updates whenever VisualTreeHelper.GetParent() changes? 如何从带有孩子的父对象列表中提取数据? - How can I extract data from a list of parent objects with children? 如何为从dependencyobject派生的类型的依赖属性设置默认值 - How can i set a default value for a dependency property of type derived from dependencyobject 如何找到没有孩子的父母记录? - How can I find parent records without children? c#查找是其他对象的子对象的对象 - c# finding objects that are children of other objects 为什么我可以访问未在DependencyObject上注册的DependencyProperties? - Why can I access DependencyProperties that are not registered on my DependencyObject? 如何将DependencyObject转换为AutomationElement? - How to convert a DependencyObject into an AutomationElement? 如何循环遍历层次结构中的所有游戏对象,包括两个场景中的禁用对象所有子对象? - How can I loop over all game objects in hierarchy including disabled objects all children too in both scenes? WPF查找元素与VisualTreeHelper垂直和水平 - WPF find element with VisualTreeHelper vertical and horizontal 如何在没有 WMI 的情况下可靠地找到应用程序安装位置 - How to reliably find app InstallLocation without WMI
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM