none
Specified element is already the logical child of another element. Disconnect it first. RRS feed

  • Question

  •  Dim listBoxParticipant As New ListBoxItem  
            Dim listBoxSelectedItem As New ListBoxItem  
     
            listBoxSelectedItem = ListBoxChatDirectory.SelectedItem  
     
            ' Produces the following error: Specified element is already the logical child of another element. Disconnect it first.  
            listBoxParticipant.Content = listBoxSelectedItem.Content  
     
            Dim windowChat As New WindowChat(listBoxParticipant)  
            WindowChat.Show() 
            <Expander Name="ExpanderChat" Header="Chat">  
                        <StackPanel Margin="10">  
     
                            <ListBox Name="ListBoxChatDirectory" Width="200" Margin="10">  
     
                                <ListBoxItem> 
                                    <StackPanel> 
                                        <StackPanel Orientation="Horizontal">  
                                            <Image Source="/Chat.ico" /> 
                                            <Label Name="LabelContactName" Content="Alicia" /> 
                                        </StackPanel> 
                                        <Label Name="LabelContactAddress" Content="aw@it.com" /> 
                                    </StackPanel> 
                                </ListBoxItem> 
     
                                <Separator Margin="10" /> 
     
                                <ListBoxItem> 
                                    <StackPanel> 
                                        <StackPanel Orientation="Horizontal">  
                                            <Image Source="/Chat.ico" /> 
                                            <Label Content="Vander" /> 
                                        </StackPanel> 
                                        <Label Content="vw@it.com" /> 
                                    </StackPanel> 
                                </ListBoxItem> 
     
                            </ListBox> 
     
                        </StackPanel> 
     
                    </Expander> 

    What I am trying to do is very very simple... I THOUGHT!

    I have a Window that accepts a ListBoxItem in the Windows constructor.

    I have tried variations of this code and nothing works.

    A few forum posts suggested I should not have to delete the ListBoxItem, but couldnt get that to work. Makes no sense why I cannot create an entirely new instance and put into a new ListBox. I have also tried the following w/o sucess:

    listBoxSelectedItem = ListBoxChatDirectory.ItemContainerGenerator.ContainerFromIndex(listBoxSelectedIndex) 

    Very simple few lines that have wasted hours of my time with worthless references on the web. Please send me something that actually works.

    Saturday, January 24, 2009 9:44 AM

Answers

All replies

  • You can't do this. The content of the selected item is part of the visual tree; it cannot belong to two listboxes at the same time.
    Controls for WPF, Windows Forms and Silverlight at http://www.divelements.co.uk
    • Marked as answer by Tao Liang Thursday, January 29, 2009 3:41 AM
    Saturday, January 24, 2009 9:58 AM
  • it is better to pass DataItem insted of the actual container

    Saturday, January 24, 2009 11:09 AM
    Moderator
  • Hi,

    Instead of creating new ListBoxItem using the SelectedItem, Why don't you pass the ListBoxChatDirectory.SelectedItem itself to the WindowChat.

     

    - Arun

    Saturday, January 24, 2009 4:26 PM
  • Tim Dawson said:

    You can't do this. The content of the selected item is part of the visual tree; it cannot belong to two listboxes at the same time.


    Controls for WPF, Windows Forms and Silverlight at http://www.divelements.co.uk



     It is obvious it cannot be done, then how do you suggest this be performed???
    Sunday, January 25, 2009 1:09 AM
  • lee d said:

    it is better to pass DataItem insted of the actual container



    How would that be done exactly? I am using a ListBox, not a gridview or whatever a DataItem you are referring to.
    Sunday, January 25, 2009 1:11 AM
  • Arun Prakash R said:

    Hi,

    Instead of creating new ListBoxItem using the SelectedItem, Why don't you pass the ListBoxChatDirectory.SelectedItem itself to the WindowChat.

     

    - Arun



    I have tried this as well. After I pass it here is my code for the WindowChat constructor

            Dim listBoxItem As New ListBoxItem  
            listBoxItem.Content = listBoxItemParticipant.Content  
            ListBoxParticipants.Items.Add(listBoxItem) 

    This returns the exact same error.

    I have also tried:

            Dim listBoxItem As New ListBoxItem  
            listBoxItem = listBoxItemParticipant  
            ListBoxParticipants.Items.Add(listBoxItem) 

    But this returns the following error:

    Element already has a logical parent. It must be detached from the old parent before it is attached to a new one.

    Sunday, January 25, 2009 1:16 AM
  • I dont need this specific code to work, but NEED a solution, I know it doesnt work. I dont understand why I cannot create a New ListBoxItem from another (in essesne copy) and assign the new one to a differant ListBox. Since I have created a new instance which has no refernce to the one it was created from.

    I have also tried to REMOVE the ListBoxItem from the parent BEFORE additing to the new ListBox, however, I get the same error.

    TIA,

    Steve

     

    Sunday, January 25, 2009 1:22 AM