I have a UserControl called ZoneContainer
. This has a property that which contains a ListBox
containing a number of ListItem
s. Each ListItem contains a DockPanel
.
I'm trying to use a the following code to find the children that exist inside ZoneContainer
but childrenCount
is 0 every time.
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.
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;