{"record":{"id":"507349e52d38ed29","repo":"dotnet/wpf","slug":"sr-notinassociatedtree-validationhelper","errorCode":null,"errorMessage":"SR.NotInAssociatedTree","messagePattern":"SR\\.NotInAssociatedTree","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/ValidationHelper.cs","lineNumber":44,"sourceCode":"        // Throws an appropriate exception if a test fails.\n        internal static void VerifyPosition(ITextContainer tree, ITextPointer position)\n        {\n            VerifyPosition(tree, position, nameof(position));\n        }\n        \n        // Verifies a TextPointer is non-null and is associated with a given TextContainer.\n        //\n        // Throws an appropriate exception if a test fails.\n        internal static void VerifyPosition(ITextContainer container, ITextPointer position, string paramName)\n        {\n            if (position == null)\n            {\n                throw new ArgumentNullException(paramName);\n            }\n\n            if (position.TextContainer != container)\n            {\n                throw new ArgumentException(SR.Format(SR.NotInAssociatedTree, paramName));\n            }\n        }\n\n        // Verifies two positions are safe to use as a logical text span.\n        //\n        // Throws ArgumentNullException if startPosition == null || endPosition == null\n        //        ArgumentException if startPosition.TextContainer != endPosition.TextContainer or\n        //                             startPosition > endPosition\n        internal static void VerifyPositionPair(ITextPointer startPosition, ITextPointer endPosition)\n        {\n            ArgumentNullException.ThrowIfNull(startPosition);\n            ArgumentNullException.ThrowIfNull(endPosition);\n            if (startPosition.TextContainer != endPosition.TextContainer)\n            {\n                throw new ArgumentException(SR.Format(SR.InDifferentTextContainers, \"startPosition\", \"endPosition\"));\n            }\n            if (startPosition.CompareTo(endPosition) > 0)\n            {","sourceCodeStart":26,"sourceCodeEnd":62,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/ValidationHelper.cs#L26-L62","documentation":"ValidationHelper.VerifyPosition throws ArgumentException(SR.NotInAssociatedTree) when the supplied position belongs to a different ITextContainer than the one passed in. TextPointers are only valid within their owning document; mixing pointers between documents is rejected.","triggerScenarios":"Passing a TextPointer from one FlowDocument/TextBox to another document's API (e.g. container.VerifyPosition(otherContainer, pointerFromElsewhere, nameof(pos))).","commonSituations":"Copying TextPointer fields between RichTextBox instances; using a saved pointer after assigning a new FlowDocument to the control; merging selections from two documents.","solutions":["Always obtain pointers from the same container you operate on (e.g. use document.ContentStart of THAT document).","Re-create pointers after swapping the control's Document; old pointers are orphaned.","Guard with pos.TextContainer == container before calling text APIs."],"exampleFix":"// before\notherDoc.TextContainer.VerifyPosition(container, pointerFromOtherDoc, nameof(pointerFromOtherDoc));\n// after\nif (pointerFromOtherDoc.TextContainer == container) container.VerifyPosition(container, pointerFromOtherDoc, nameof(pointerFromOtherDoc));","handlingStrategy":"type-guard","validationCode":"bool sameTree = position != null && position.TextContainer == container;","typeGuard":"bool BelongsToContainer(ITextPointer p, ITextContainer c) => p != null && p.TextContainer == c;","tryCatchPattern":"try { container.VerifyPosition(container, pos, nameof(pos)); }\ncatch (ArgumentException) { pos = container.CurrentPosition; }","preventionTips":["Only use pointers from the same document","Recreate pointers after Document swaps","Never share TextPointers across controls"],"tags":["wpf","textpointer","argument","type-mismatch"],"backgroundTag":"type-mismatch","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"}