I’m trying to insert multiple rectangles in a stackpanel but I keep getting the error ‘Element is already the child of another element.’. Same thing happens if I use a canvas.
Example:
List<Rectangle> recList = new List<Rectangle>();
…put some rectangles in the list
StackPanel stack = new StackPanel();
foreach(var item in recList)
stack.Children.Add(item); // get error here on 2nd item trying to add
uiStackPanel.Children.Add(stack); // declared in XAML
I want to be able to insert rectangles dynamically in a horizontal orientation.
According to the Internet I should be able to do this (manually at least) but…
What to do, what to do? 🙂
Seems like you are adding the same rectangle more than once.
If you need to add different rectangles than the code would be like this:
This code works.