tags:

views:

29

answers:

1

How do you access the children of a WPF Canvas class?

It is a cool class and I like how you are able to add children. But once they are there, how to you look at them for reading their state and content. I know it is easy if you put the children in XAML. But what if you have added children to the canvas dynamically at run time?

+1  A: 

You can use the Children property.


Edit: Here is an example of iterating over the children and getting one by index:

foreach (UIElement child in canvas.Children)
{
    // ...
}
// Or: 
int index = 0;
var childByIndex = canvas.Children[index];
Quartermeister
I saw that but it only talks about Adding the Children, not viewing them.
xarzu
@xarzu: UIElementCollection implements IList, so you can enumerate it using Linq or a foreach loop and you can get children by index using the indexer.
Quartermeister
What is the menber of the canvas class that indicates the number of children? Something like canvas.amount, for example.
xarzu
@xarzu: canvas.Children.Count
Quartermeister