C# 指定的元素已经是另一个元素的逻辑子元素。先断开

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9109738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 05:59:30  来源:igfitidea点击:

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

c#wpfframeworkelement

提问by Guillaume Slashy

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

这是我想将 FrameworkElement 附加到新窗口以将其发布到 PNG 文件时遇到的错误。

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 ?

我在 SO 上查看了很多有关此错误的问题,但没有人回答我的问题我错过了什么?

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();

采纳答案by Clemens

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

如果element是面板(例如网格)的子项,则必须将其从面板的子项集合中删除。如果它设置为Contenta ContentControl,则必须将该 Content 设置为 null (或其他任何不是element)。

回答by Piotr Justyna

Guillaume,

纪尧姆,

You can try to additionally use RemoveVisualChildmethod after RemoveLogicalChild:

您可以尝试在RemoveLogicalChild之后额外使用RemoveVisualChild方法:

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

Hope this helps, Piotr.

希望这会有所帮助,Piotr。

回答by Gerald G.

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).

我有类似但略有不同的问题,但收到相同的错误消息。我通过创建一个继承类并调用 RemoveLogicalChild (因为这是一个受保护的方法)来解决方法。

 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

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

回答by Jamie Mellway

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);
            }
        }
    }
}