elecman
Topic Author
Posts: 90
Joined: 20 Jul 2014, 04:28

Add child to specific location in tree

22 Dec 2018, 04:54

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);
 
User avatar
sfernandez
Site Admin
Posts: 3005
Joined: 22 Dec 2011, 19:20

Re: Add child to specific location in tree

24 Dec 2018, 12:14

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);
 
elecman
Topic Author
Posts: 90
Joined: 20 Jul 2014, 04:28

Re: Add child to specific location in tree

25 Dec 2018, 08:29

Ok, thanks!
 
User avatar
jsantos
Site Admin
Posts: 3935
Joined: 20 Jan 2012, 17:18
Contact:

Re: Add child to specific location in tree

07 Jan 2019, 17:42

Thanks for your feedback, marking this as solved.

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 3 guests