“元素已经是另一个元素的子元素." [英] "Element is already the child of another element."

查看:23
本文介绍了“元素已经是另一个元素的子元素."的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

起初,这个例外对我来说没有意义.为什么我不能多次复制这个对象?但这不是重点:

At first, this exception doesn't really make sense to me. Why shouldn't i be able to duplicate this object multiple times? but thats not the point:

我使用列表.每当我导航到一个网站时,它应该这样做:

i use a List. Whenever i navigate to a site, it should do this:

 (App.Current as App).recent.ForEach(x => container.Children.Add(x));

(容器 = 另一个 StackPanel)

(container = another StackPanel)

第一次,它有效.之后,我得到了问题标题中显示的异常.我已经尝试使用列表框,但我刚刚收到一个 ArgumentException.我认为这些异常具有相同的来源,但我不知道我做错了什么.请帮忙

the first time, it works. afterwards, i get the exception displayed in the questiontitle. i already tried using a listbox, but i just got a ArgumentException. I think these exceptions have the same source, but i don't know what i'm doing wrong. please help

谢谢

推荐答案

错误很明显:一个 WPF/SL 控件一次只能属于 1 个父控件.

The error is quite clear: A WPF/SL Control can only belong to 1 Parent control at a time.

因此,当您离开页面时,您要么必须从其父级中删除控件,要么必须在此 ForEach 中创建(可能克隆)新控件.

So you'll either have to remove the Controls from their Parent when you are moving away from a Page or you'll have to Create (possibly Clone) new Controls in this ForEach.

这篇关于“元素已经是另一个元素的子元素."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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