{"record":{"id":"d8c6fb7d20a10097","repo":"dotnet/wpf","slug":"sr-format-sr-textelementcollection-d8c6fb","errorCode":null,"errorMessage":"SR.Format(SR.TextElementCollection_NextSiblingDoesNotBelongToThisCollection, nextSibling.GetType().Name)","messagePattern":"SR\\.Format\\(SR\\.TextElementCollection_NextSiblingDoesNotBelongToThisCollection, nextSibling\\.GetType\\(\\)\\.Name\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextElementCollection.cs","lineNumber":287,"sourceCode":"        /// <summary>\n        /// Inserts a TextElement newItem into a collection before a nextSibling TextElement.\n        /// </summary>\n        /// <param name=\"nextSibling\">\n        /// TextElement before which the newItem is to be inserted\n        /// </param>\n        /// <param name=\"newItem\">\n        /// A TextElement to be inserted into the collection before the nextSibling.\n        /// It must be unlinked from a tree before insertion.\n        /// </param>\n        public void InsertBefore(TextElementType nextSibling, TextElementType newItem)\n        {\n            ArgumentNullException.ThrowIfNull(nextSibling);\n\n            ArgumentNullException.ThrowIfNull(newItem);\n\n            if (nextSibling.Parent != this.Parent)\n            {\n                throw new InvalidOperationException(SR.Format(SR.TextElementCollection_NextSiblingDoesNotBelongToThisCollection, nextSibling.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(nextSibling.ElementStart);\n            }\n            finally\n            {\n                this.TextContainer.EndChange();\n            }","sourceCodeStart":269,"sourceCodeEnd":305,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextElementCollection.cs#L269-L305","documentation":"Thrown by TextElementCollection<T>.InsertBefore as an InvalidOperationException when the nextSibling reference does not belong to this collection: its Parent differs from the collection's Parent. InsertBefore inserts relative to a sibling, so the anchor must already live in the same parent element.","triggerScenarios":"Calling InsertBefore(nextSibling, newItem) where nextSibling.Parent != this.Parent — e.g. the anchor element is a child of a different Paragraph/TextElement or is unparented (null).","commonSituations":"Passing an anchor run from another paragraph; passing an element that was just removed from the collection; mixing up which container owns the anchor.","solutions":["Use a nextSibling whose Parent equals this collection's Parent object.","If the anchor is unattached, insert it first (Add) or compute a different insertion position (e.g. use TextPointer-based insertion).","Verify you are calling InsertBefore on the collection of the same parent that owns nextSibling."],"exampleFix":"// before\nwrongParagraph.Inlines.InsertBefore(runInOtherPara, newRun);\n// after\nvar host = (Paragraph)runInOtherPara.Parent;\nhost.Inlines.InsertBefore(runInOtherPara, newRun);","handlingStrategy":"validation","validationCode":"if (nextSibling != null && nextSibling.Parent == collection.Parent)\n    collection.InsertBefore(nextSibling, newItem);","typeGuard":"bool AnchorIsValid(TextElementType anchor, object collectionParent) => anchor?.Parent == collectionParent;","tryCatchPattern":"try { collection.InsertBefore(anchor, item); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"does not belong\")) { /* pick an anchor owned by this parent */ }","preventionTips":["Derive the anchor from the same parent you are inserting into ((T)anchor.Parent).","Never hold anchor references across container boundaries.","Re-query the anchor after any structural change to the tree."],"tags":["wpf","text-tree","invalid-state","sibling-anchor"],"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-21T21:30:21.729Z"}