Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to loop through WPF StackPanel static Items?

Probably very easy but I am having trouble to figure this out (also Google doesn't seem to help much).

How can I loop through the statically declared elements (no databinding - elements are declared in the xaml) of a StackPanel?

Any help appreciated!

like image 418
JohnIdol Avatar asked Sep 14 '09 10:09

JohnIdol


1 Answers

Do you mean the StackPanel's children?

foreach (var child in stackPanel.Children)
{
    //do something with child
}

A more generic solution that would work regardless of the parent would be to use LogicalTreeHelper or VisualTreeHelper, depending on what WPF tree you wish to traverse:

foreach (var child in LogicalTreeHelper.GetChildren(stackPanel))
{
    //do something with child
}
like image 50
Kent Boogaart Avatar answered Oct 19 '22 23:10

Kent Boogaart