views:

588

answers:

1

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!

+1  A: 

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
}

HTH, Kent

Kent Boogaart
thanks - just found it myself but your answer is spot-on. couldn't find it as I was looking for "Items"!
JohnIdol
While Kent is 100% correct, if you're editing XAML objects directly in your code-behind, it's most likely the case that you're not using databinding where you should be. Not always the case though :)
Paul Betts