I\'m using VisualTreeHelper.GetChildrenCount()
to find child controls, but it always return 0.
Here is my code
The problem is that when the ItemContainerGenerator
signals the ContainersGenerated
status, the container (a ContentPresenter
) has been created, but not yet loaded. Especially the data template has not yet been applied to the ContentPresenter, hence there is nothing in the visual tree.
You may get around this by adding a Loaded
event handler when looping over the generated containers.
private void ItemContainerGeneratorStatusChanged(object sender, EventArgs e)
{
if (itemsControl.ItemContainerGenerator.Status
== GeneratorStatus.ContainersGenerated)
{
var containers = itemsControl.Items.Cast