Kostadin Dlagnekov
Kostadin Dlagnekov

Reputation: 23

Manipulating canvas children C# WPF

I can't find way to manipulating canvas children UI elements which been add programmatically. This is my code for add children in canvas. I want to manipulating every of those canvas children in runtime.

There are any way to do this?

Canvas = new Canvas();
canvas.Width = mapArea.ActualWidth;
canvas.Height = mapArea.ActualHeight;
mapArea.Children.Add(new UIElement() { Uid = "Camera" + index });

Image imageB = new Image();
{
    imageB.Width = 60;
    imageB.Source = camera;
}

mapArea.Children.Add(imageB);
Canvas.SetTop(imageB, PosX);
Canvas.SetLeft(imageB, PosY);

Upvotes: 1

Views: 4136

Answers (1)

Tim Rutter
Tim Rutter

Reputation: 4679

Depends what you want to do to each child. Say you want to turn all Path objects red

foreach (var c in canvas.Children.OfType<Path>())
{
    c.Stroke = Brushes.Red;
}

Or change the width of all the images:

foreach (var c in canvas.Children.OfType<Image>())
{
    c.Width= 10;
}

Or if you know that a child of a specific type exists at a specific index you can simply do:

var child = (Image)canvas.Children.ElementAt(3) ;

Upvotes: 2

Related Questions