How do I access the children of an ItemsControl?

James Hay picture James Hay · Jun 16, 2009 · Viewed 21k times · Source

If i have a component derived from ItemsControl, can I access a collection of it's children so that I can loop through them to perform certain actions? I can't seem to find any easy way at the moment.

Answer

Thomas Levesque picture Thomas Levesque · Jun 16, 2009

A solution similar to Seb's but probably with better performance :

for(int i = 0; i < itemsControl.Items.Count; i++)
{
    UIElement uiElement =
        (UIElement)itemsControl.ItemContainerGenerator.ContainerFromIndex(i);
}