title: Specified element is already the logical child of another element. Disconnect it first
body: <p>here is the error I have when I want to attach a FrameworkElement to a new Window to publish it to a PNG file.</p>
  
  <p>So my idea is to remove the parent-child link, call my method, and add the child again with this code :</p>
  
  <pre><code>this.RemoveLogicalChild(element);
  PublishFrameworkElement(element, stream);
  this.AddLogicalChild(element);
  </code></pre>
  
  <p>But I got the exact same error...</p>
  
  <p>I looked a lot of questions about this error, here on SO, but none answered to my problem
  What am I missing ?</p>
  
  <p>EDIT : here is the code that worked for me :</p>
  
  <pre><code>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();
  </code></pre>