Search code examples
c#windows-phone-7stackpanel

"Element is already the child of another element."


At first, this exception doesn't really make sense to me. Why shouldn't i be able to duplicate this object multiple times? but thats not the point:

i use a List. Whenever i navigate to a site, it should do this:

 (App.Current as App).recent.ForEach(x => container.Children.Add(x));

(container = another StackPanel)

the first time, it works. afterwards, i get the exception displayed in the questiontitle. i already tried using a listbox, but i just got a ArgumentException. I think these exceptions have the same source, but i don't know what i'm doing wrong. please help

thanks


Solution

  • The error is quite clear: A WPF/SL Control can only belong to 1 Parent control at a time.

    So you'll either have to remove the Controls from their Parent when you are moving away from a Page or you'll have to Create (possibly Clone) new Controls in this ForEach.