I built a new FlowDocument Paragraph B by examining/using elements of an existing Paragraph A. To my surprise the elements I added to the new ParagraphB were magically deleted from ParagraphA. I created a simple illustration below. After the 3rd line executes the myRun element will be removed from myParagraphA.
1) How? What is the underlying mechanism that enables myParagraphA to delete myRun from its inline collection?
2) Why? I assume the designers did not want an element to have 2 parents.
3) If my observations are correct I guess I must add a copy of myRun to myParagraphB to avoid destroying myParagraphA. What is the best way to copy myRun with its text and properties (Cloning)? This is a performance hit since I actually will do this operation a lot.
var myRun = new Run("Hello");
var myParagraphA = new Paragraph(myRun);
var myParagraphB = new Paragraph(myRun);
Thanks,
It’s quite common that an item can only be in one list at a time. You’ll usually see a Parent property (note: singular). This is the case for XmlElement, TreeViewItem etc.
So by adding the Run to myParagraphB you overwrite its Parent property, and proper coding of that property removes it from myParagraphA’s list of inlines.
The solution is indeed: Cloning.