[英]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. 它具有一个属性,该属性包含一个包含多个
ListItem
的ListBox
。 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.