Page 1 of 1

Add child to specific location in tree

Posted: 22 Dec 2018, 04:54
by elecman
I can add a new element to a Canvas using canvas.Children.Add()
But how do I place that element above or below an existing element in the xaml tree?

Is this the way it is supposed to be done?
int index = canvas.Children.IndexOf(existingElement);
canvas.Children.Insert(index + 1, newElement);

Re: Add child to specific location in tree

Posted: 24 Dec 2018, 12:14
by sfernandez
Yes, you can use Insert to add a child in a specific order.
This will affect the logical order and also how children are rendered.

If you only care about render order you can then use Panel.ZIndex property (higher value indicates that is rendered later):
Panel.SetZIndex(newElement, 10);
Panel.SetZIndex(existingElement, 20);
canvas.Children.Add(newElement);

Re: Add child to specific location in tree

Posted: 25 Dec 2018, 08:29
by elecman
Ok, thanks!

Re: Add child to specific location in tree

Posted: 07 Jan 2019, 17:42
by jsantos
Thanks for your feedback, marking this as solved.