{"record":{"id":"00202713c0c0b6fd","repo":"dotnet/wpf","slug":"sr-format-sr-textelementcollection","errorCode":null,"errorMessage":"SR.Format(SR.TextElementCollection_PreviousSiblingDoesNotBelongToThisCollection, previousSibling.GetType().Name)","messagePattern":"SR\\.Format\\(SR\\.TextElementCollection_PreviousSiblingDoesNotBelongToThisCollection, previousSibling\\.GetType\\(\\)\\.Name\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextElementCollection.cs","lineNumber":248,"sourceCode":"        /// <summary>\n        /// Inserts a unlinked TextElement newItem after previousSibling, which is supposed to be an existing member of this collection.\n        /// </summary>\n        /// <param name=\"previousSibling\">\n        /// TextElement after which the newItem is to be inserted\n        /// </param>\n        /// <param name=\"newItem\">\n        /// A TextElement to be inserted into the collection after the previousSibling.\n        /// It must be unlinked from a tree before insertion.\n        /// </param>\n        public void InsertAfter(TextElementType previousSibling, TextElementType newItem)\n        {\n            ArgumentNullException.ThrowIfNull(previousSibling);\n\n            ArgumentNullException.ThrowIfNull(newItem);\n\n            if (previousSibling.Parent != this.Parent)\n            {\n                throw new InvalidOperationException(SR.Format(SR.TextElementCollection_PreviousSiblingDoesNotBelongToThisCollection, previousSibling.GetType().Name));\n            }\n\n            if (newItem.Parent != null)\n            {\n                throw new ArgumentException(SR.Format(SR.TextSchema_TheChildElementBelongsToAnotherTreeAlready, this.GetType().Name));\n            }\n\n            ValidateChild(newItem);\n\n            this.TextContainer.BeginChange();\n            try\n            {\n                newItem.RepositionWithContent(previousSibling.ElementEnd);\n            }\n            finally\n            {\n                this.TextContainer.EndChange();\n            }","sourceCodeStart":230,"sourceCodeEnd":266,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextElementCollection.cs#L230-L266","documentation":"TextElementCollection<T>.InsertAfter requires the previousSibling to belong to the same collection (same Parent). If previousSibling.Parent differs from this collection's Parent, it throws InvalidOperationException saying the previous sibling does not belong to this collection.","triggerScenarios":"Calling InsertAfter(newItem, previousSibling) where previousSibling lives under a different parent element (e.g. inserting into Paragraph B's Inlines using an element from Paragraph A).","commonSituations":"Copy/paste and drag-drop code moving elements between FlowDocument containers; reordering logic that assumes a sibling is in the target collection.","solutions":["Use a previousSibling that is actually a member of the target collection (check its Parent first).","Move previousSibling into the target collection first, then InsertAfter.","Use Add/AddBefore on the correct collection instead."],"exampleFix":"// before\ntargetInlines.InsertAfter(newItem, itemFromOtherParagraph);\n// after\nif (itemFromOtherParagraph.Parent == targetParagraph)\n    targetInlines.InsertAfter(newItem, itemFromOtherParagraph);\nelse\n    targetInlines.Add(newItem);","handlingStrategy":"validation","validationCode":"bool siblingInCollection = previousSibling != null && previousSibling.Parent == targetParent;\nif (!siblingInCollection) throw new ArgumentException(\"previousSibling must belong to the target collection\");","typeGuard":null,"tryCatchPattern":"try { targetInlines.InsertAfter(newItem, previousSibling); }\ncatch (InvalidOperationException) { targetInlines.Add(newItem); }","preventionTips":["Confirm previousSibling.Parent matches the target's parent","Re-lookup siblings from the target collection after moves","Use collection membership checks before insert operations"],"tags":["wpf","collection","argument"],"backgroundTag":"invalid-argument-value","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}