指定的元素已经是另一个元素的逻辑子。首先将其断开 [英] Specified element is already the logical child of another element. Disconnect it first

查看:1816
本文介绍了指定的元素已经是另一个元素的逻辑子。首先将其断开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,当我想一个FrameworkElement的附加到一个新的窗口将其发布到一个PNG文件,我有错误。



所以我的想法是删除亲子环节,请拨打我的方法,以及与此代码重新添加子:

  this.RemoveLogicalChild(元); 
PublishFrameworkElement(元素,流);
this.AddLogicalChild(元);



但我得到了完全相同的错误...



我看了很多问题关于这个错误,在这里SO,但没有回答我的问题
我失去了什么。



编辑?这里是为我工作的代码:

  VAR元素= _GeneratedContent为FrameworkElement的; 
VAR ParentPanelCollection =(element.Parent作为面板)。孩子的UIElementCollection;
ParentPanelCollection.Clear();

的FileStream流= [...]

如果(元素!= NULL)
{
PublishFrameworkElement(元素,流);
ParentPanelCollection.Add(元);
}
stream.Close();


解决方案

如果元素是面板(如网格),你必须从小组的儿童集合。如果设置为内容 ContentControl中,你必须设置内容为空(或其他任何这不是元素)。


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

解决方案

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

这篇关于指定的元素已经是另一个元素的逻辑子。首先将其断开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆