WPF Error: Specified element is already the logical child of another element. Disconnect it first.

The first time I saw this error, it completely threw me off. I had a ContentControl and in the style for it I was setting the Content to a wrapper for the ContentPresenter.

<Style x:Key="MyCustomContentControl" 
       TargetType="{x:Type ContentControl}">
    <Setter Property="Content">
        <Setter.Value>
            <local:MyCustomControl>
                <ContentPresenter />
            </local:MyCustomControl>
        </Setter.Value>
    </Setter>
</Style>

I would get this error whenever more than one ContentControl implemented this style.

I finally realized the error was telling me exactly what the issue was, I was just too blind to realize it. I was setting the Content, not the ContentTemplate. The specified element (MyCustomControl) was already the child of one ContentControl, so I could not add it as a child to another one.

I needed to switch my style to set the ContentTemplate, not the Content of my ContentControl. The way I currently had it written was like giving multiple people the same cookie to eat. It just didn’t work. Instead I needed to give them the cookie cutter so they could all make their own cookies, which is why I needed to set the Template instead of the Content.

<Style x:Key="MyCustomContentControl" 
       TargetType="{x:Type ContentControl}">
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <local:MyCustomControl>
                <ContentPresenter />
            </local:MyCustomControl>
        </Setter.Value>
    </Setter>
</Style>

7 Responses to WPF Error: Specified element is already the logical child of another element. Disconnect it first.

  1. Allen says:

    Please: What does it look like when you fixed it? a bit more example?
    Thank you.

  2. Daniel says:

    Ok, so I had this error with a DataGrid, and it boiled down to the same issue as what was described here: http://impulsecode.blogspot.com/2012/01/in-wpf-getting-error-specified-element.html.

    However, I have no real idea why the same error should occur in this case. Any ideas?

    • Rachel says:

      Hi Daniel,

      The same problem is occurring. The DataGrid is setting the Header property to a single instance of an element, however if WPF tries to display a 2nd copy of the header, or if it tries to display the header using a different parent element, then the error will occur because an object in WPF can only belong to one parent object at a time. I suspect it’s the 2nd issue that’s occurring – trying to add the header object to a new parent element without removing it from it’s old parent element first.

      The solution is to use a HeaderTemplate, which will create new copies of the element anytime it is needed instead of trying to reuse a single element.

      • Daniel says:

        Rachel,

        It seems then that this is actually a bug in the DataGrid itself, as I would think that the framework should remove the header object from the old parent before adding it to the new parent. There’s no way we would be expected to do this ourselves…

        Daniel

  3. amit says:

    Hi Rachel,
    this is a nice article and although you have addressed nicely yet it seems different from case to case or maybe because I’m new to WPF!

    Long story short, I’m getting this error since I’m adding a toolboxitem to a toolbar .. of course not because of adding but because I’m assigning a content to the toolboxitem as following:

    var MyControl = this.FindResource(“myControl1″) as ContentControl;
    var objToolBar = ToolboxContainer.Content as Toolbox;
    ToolboxItem objToolboxItem = new ToolboxItem();
    ToolboxItem o = new ToolboxItem();
    Border border = null;

    border = new Border();
    border.IsHitTestVisible = false;
    border.Tag = txtSearch.Text.Trim();

    objToolboxItem.Content = MyControl1; //***this is causing the error***
    objToolboxItem.ToolTip = txtSearch.Text.Trim();
    objToolBar.Items.Add(objToolboxItem);

    and in XAML I have:

    Base on your comments I thought maybe I have to change Style1 since I’m assigning it to objToolboxItem.Content however, in Style1 ContentTemplate is already in use.

    Any suggestions will be appreciated.

    Thanks,
    amit

    • Rachel says:

      Hi Amit,

      It sounds like MyControl1 already exists somewhere in your VisualTree, and has a Parent already. You need to either create a brand new copy of MyControl1 to add it to your toolbar, or remove MyControl1 from whatever control currently has it before trying to assign it to the toolbar’s Content.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 109 other followers

%d bloggers like this: