I have a list box that I’m trying to populate with a list of viewboxes.
The listbox takes in the list without a problem. However, when my function reaches its end, I receive the error:
“Must disconnect specified child from current parent Visual before attaching to new parent Visual.”
The viewboxes are created from the same initial viewbox and then edited, which I believe may be my problem.
for(...)
{
Viewbox newviewbox = (Viewbox)myViewbox; //myViewbox created in XAML
// edits newviewbox here
viewboxlist.Add(newviewbox); //viewboxlist created upon initialization
newviewbox = null;
}
myListBox.ItemsSource = viewboxlist;
Any advice is greatly appreciated.
Thanks.
You are not creating new viewboxes, you are just referencing the same viewbox and adding it to the list several times. Thus the error that the viewbox already have a parent and should be disconnected before assigning it to a new parent.
You should create new instances of ViewBox like this: