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

39,143

Solution 1

If element is the child of a Panel (e.g. Grid) you have to remove it from the Panel's Children collection. If it is set as Content of a ContentControl, you'd have to set that Content to null (or anything else that is not element).

Solution 2

Guillaume,

You can try to additionally use RemoveVisualChild method after RemoveLogicalChild:

this.RemoveLogicalChild(element);
this.RemoveVisualChild(element);
PublishFrameworkElement(element, stream);

Hope this helps, Piotr.

Solution 3

Old question but I didn't have luck with the other answers, so I made a extension method to remove the item from its parent.

public static class FrameworkElementHelper
{
    public static void RemoveFromParent(this FrameworkElement item)
    {
        if (item != null)
        {
            var parentItemsControl = (ItemsControl)item.Parent;
            if (parentItemsControl != null)
            {
                parentItemsControl.Items.Remove(item as UIElement);
            }
        }
    }
}

Solution 4

I had similar but slightly different issue but got the same error message. I made a workaround by making an inherited class and calling RemoveLogicalChild (since this is a protected method).

 public partial class PopupWindow : Window
{
    public PopupWindow()
    {
        InitializeComponent();
    }

    protected override void OnClosed(EventArgs e)
    {
        base.OnClosed(e);
        this.RemoveLogicalChild(this.Content);    // since protected method
    }
}

It worked for me. I made a simple example you can see here.

http://wpfgrid.blogspot.com/2013/01/wpf-error-specified-element-is-already.html

Share:
39,143

Related videos on Youtube

Guillaume Slashy
Author by

Guillaume Slashy

Software Developer

Updated on November 21, 2020

Comments

  • Guillaume Slashy
    Guillaume Slashy over 3 years

    here is the error I have when I want to attach a FrameworkElement to a new Window to publish it to a PNG file.

    So my idea is to remove the parent-child link, call my method, and add the child again with this code :

    this.RemoveLogicalChild(element);
    PublishFrameworkElement(element, stream);
    this.AddLogicalChild(element);
    

    But I got the exact same error...

    I looked a lot of questions about this error, here on SO, but none answered to my problem What am I missing ?

    EDIT : here is the code that worked for me :

    var element = _GeneratedContent as FrameworkElement;
    var ParentPanelCollection = (element.Parent as Panel).Children as UIElementCollection;
    ParentPanelCollection.Clear();
    
    FileStream stream = [...]
    
    if (element != null)
    {
        PublishFrameworkElement(element, stream);
        ParentPanelCollection.Add(element);
    }
    stream.Close();
    
  • Guillaume Slashy
    Guillaume Slashy over 12 years
    I tried it before but this give me this error : "Specified Visual is not a child of this Visual."
  • Piotr Justyna
    Piotr Justyna over 12 years
    Guillaume, the code I've posted doesn't throw this exception, so maybe you're using it in a different context. Please post some more code and describe your problem with more detail so it's easier for us to help you.
  • Piotr Justyna
    Piotr Justyna over 12 years
    Additionally, what is "this" in your case?
  • Rauld
    Rauld almost 12 years
    would setting to null, disconnect events as well ?
  • Clemens
    Clemens almost 12 years
    No, you would also have to remove any event handlers.
  • Markus Hütter
    Markus Hütter about 4 years
    Additionally: if you have a Decorator like Border you can simply set it's Child=null
  • RAMM-HDR
    RAMM-HDR almost 2 years
    > Usage Note: change ( ItemsControl ) to the type of ( Element parent ) you have, for example if you have < StackPanel /> then change it to (StackPanel) , and changed Items.Remove to Children.Remove in the case of (StackPanel) . and so on, Apply any changes that fit the Element parent of your choice if needed