I want to dynamically copy or move TabItems from one TabControl to another TabControl. Unfortunately, I get the following exception:
{“Element already has a logical parent. It must be detached from the old parent before it is attached to a new one.”}
Trying to delete it from the old TabControl before adding it to the new throws off my iterator.
I guess you’re using a foreach block to find the tab to remove. You cannot modify a collection while iterating through it. So use a for loop instead to remove and cache the tab to remove.
Then add it to the new parent. That should work.
Not the most elegant, but it’d have to do for now. For some reason, Linq hasn’t implemented the Lisp/Ruby/et.all reject operation. (or I haven’t found it yet. Closest thing is a
List<T>.RemoveAll(predicate). If it did – you could have done something like…