我的页面上有两个RichTextbox。我将它们用作Book界面。有两个按钮可以前后转动。
遍历页面可以正常工作。但是一旦我回击,VS2010就会出现异常
Element is already the child of another element.
这是我在RichTextbox中添加的段落。即使我在使用新数据充斥它们之前清除了RichTextbox,也会发生这种情况。
我的代码如下。我错过了什么?
TxtBlobLeft.Blocks.Clear();
TxtBlobRight.Blocks.Clear();
foreach (Paragraph item in pagesStack[count].paras)
{
TxtBlobLeft.Blocks.Add(item); //Throws exception here
}
foreach (Paragraph item in pagesStack[count + 1].paras)
{
TxtBlobRight.Blocks.Add(item);
}
注意:pagesStack是一个自定义对象,其中包含Paras(即List)作为属性。
答案 0 :(得分:2)
似乎Clear不会删除Paragraph的父引用到RTB。我使用以下forach循环创建了一个Clear方法来删除所有项目。
foreach (Paragraph item in paras)
{
Page1.Blocks.Remove(item);
}
para是我之前添加到RTB的段落列表。
答案 1 :(得分:0)
当你输入循环时,你能检查调试器的项目的父项吗?