ChrisQuesaada

Hi,

System.InvalidOperationException was unhandled
Message="Element already has a logical parent. It must be detached from the old parent before it is attached to a new one."

This is the error, It happens when I Make some modifications to the TreeViewItems in my TreeView. I happen too when I use 3D Elements like ModelVisual3D, but in this case I clone them and no more problem.

I cant do the same with the TreeViewItems, What can I Do the fix the problem. I'm working in c# (WPF)

Thanks,

Christopher Quesada

Microsoft Student Partner




Re: Windows Presentation Foundation (WPF) Always the Same Problem --> Element already has a logical parent

Dr. WPF

What exactly are you doing that causes the exception Precisely what line of code

The exception is pretty self explanatory. Something you're doing is causing a framework element to call AddLogicalChild() to add an element as a logical child, but that element is already the logical child of another framework element.






Re: Windows Presentation Foundation (WPF) Always the Same Problem --> Element already has a logical parent

ChrisQuesaada

ok, I have a custom TreeViewItem, and I add it some properties, is simple.

I have some data in textboxes and add this data to my properties in the custom TreeViewItem.

The exception happen when I want to insert the data to my custom TreeViewItem... I make something like this:

private void MyMethod()

{

TreeViewItem Temp = new TreeViewItem();

Temp = TreeView.Items[0];

//My Custom TreeViewItem

TreeViewItem3D MyItem = new TreeViewItem3D();

MyItem = Temp.Items[index];

//My Properties

MyItem.ModelName = "Car";

MyItem.ModelDescription = "Nice Car";

//Now I need to put this Item in the TreeView again

Temp.Items[index] = MyItem;

TreeView.Items[0] = Temp;

}

What is wrong!!!

Thanks,

Chris






Re: Windows Presentation Foundation (WPF) Always the Same Problem --> Element already has a logical parent

Laurent Bugnion

Hi,

You create a new TreeViewItem, but then you discard it, and use the Temp variable to store a reference to an already existing TreeviewItem:

Temp = TreeView.Items[0];

The line above gets a reference to the first item in the tree. Obviously, this item already has a parent, since it is already in the treeview.

Then, when you try to add the item to the treeview, it doesn't work, since the item already has a parent.

Does it help

Laurent





Re: Windows Presentation Foundation (WPF) Always the Same Problem --> Element already has a logical parent

Dr. WPF

Absolutely.

Additionally, you typically would not want to assume that the objects in the Items collection are of type TreeViewItem. Instead, you should use the ItemContainerGenerator to get at the TreeViewItem. This will then work when consumers of your control use it in a databinding scenario where the containers are auto-generated.

For example, to get the TreeViewItem for item 0, you would use the following:

Code Block

Temp = TreeView.ItemContainerGenerator

.ContainerFromIndex(0) as TreeViewItem;

Of course, you are manually stuffing your TreeViewItems into the Items collection, so you may not wish to support data binding scenarios. Still, I would encourage you to look for a solution that supports WPF's content model that lets you stuff arbitrary objects into the logical tree and get template support for them.






Re: Windows Presentation Foundation (WPF) Always the Same Problem --> Element already has a logical parent

ChrisQuesaada

Ok, with TreeView.ItemContainerGenerator.ContainerFromIndex(0) as TreeViewItem;

The changes that I made to the TreeViewItem Temp can be save in the same TreeViewItem Index of my TreeView. Avoiding the error Element already has a logical parent.

Explain me the advantajes of this method: .ItemContainerGenerator.ContainerFromIndex(0)

Thanks,

Chris






Re: Windows Presentation Foundation (WPF) Always the Same Problem --> Element already has a logical parent

Dr. WPF

> Explain me the advantages of this method

An ItemsControl generates wrappers for its items. If its a TreeView, then each wrapper it generates is of type TreeViewItem. If its a ListBox, then a wrapper is of type ListBoxItem.

If you add a TreeViewItem directly to the TreeView.Items collection, then the ItemContainerGenerator will *not* generate a wrapper because it sees that the item is already the correct type. But if you add anything else to the Items collection (e.g., a Button, a business object, an int, etc), the ItemContainerGenerator will create the TreeViewItem and set its Content to the data item (or a templated representation of the item).

In most scenarios where you are binding an ItemsControl to an observable collection of items, the type of the item will *not* be the same as the container type. In such cases, you must use the ItemContainerGenerator methods to get at the containers. The Items collection itself can contain arbitrary objects.

So the advantages of using the ItemContainerGenerator.ContainerFrom*() methods are that you are writing code that works in *all* scenarios.






Re: Windows Presentation Foundation (WPF) Always the Same Problem --> Element already has a logical parent

ChrisQuesaada

DR WPF,

thanks a lot

Regards,

Christopher Quesada

Microsoft Student Partner