tags:

views:

100

answers:

1

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.

+1  A: 

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...

tabControl2.Items.AddRange( tabControl1.Items.Reject(tab => tab.Name = "MarkedOne") );
Gishu
Isn't there a more elegant way of doing this?
Joel Rodgers
@joeb:) See update. Code doesn't render right in comments.
Gishu
Yeah, there's only the ugly way to do it by using a for loop. It can be easily converted into a linq expression though.
Joel Rodgers