{"record":{"id":"cd6d25bd547f6ba1","repo":"dotnet/wpf","slug":"sr-format-sr-notinassociatedtree-textpointer","errorCode":null,"errorMessage":"SR.Format(SR.NotInAssociatedTree, \"textPointer\")","messagePattern":"SR\\.Format\\(SR\\.NotInAssociatedTree, \"textPointer\"\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/TextAnchor.cs","lineNumber":105,"sourceCode":"        //\n        //  Public Methods\n        //\n        //------------------------------------------------------\n\n        #region Public Methods\n\n        /// <summary>\n        /// Determines if the text pointer is contained by one of the\n        /// anchor's TextSegment.s\n        /// </summary>\n        /// <param name=\"textPointer\">text pointer to test</param>\n        internal bool Contains(ITextPointer textPointer)\n        {\n            ArgumentNullException.ThrowIfNull(textPointer);\n\n            if (textPointer.TextContainer != this.Start.TextContainer)\n            {\n                throw new ArgumentException(SR.Format(SR.NotInAssociatedTree, \"textPointer\"));\n            }\n\n            // Correct position normalization on range boundary so that\n            // our test would not depend on what side of formatting tags\n            // pointer is located.\n            if (textPointer.CompareTo(this.Start) < 0)\n            {\n                textPointer = textPointer.GetInsertionPosition(LogicalDirection.Forward);\n            }\n            else if (textPointer.CompareTo(this.End) > 0)\n            {\n                textPointer = textPointer.GetInsertionPosition(LogicalDirection.Backward);\n            }\n\n            // Check if at least one segment contains this position.\n            for (int i = 0; i < _segments.Count; i++)\n            {\n                if (_segments[i].Contains(textPointer))","sourceCodeStart":87,"sourceCodeEnd":123,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/TextAnchor.cs#L87-L123","documentation":"TextAnchor.Contains verifies that the given ITextPointer belongs to the same TextContainer as the anchor's Start pointer before comparing positions. WPF throws this ArgumentException when a pointer from a different document/tree is passed, because positional comparison across containers is meaningless. It is an API-contract check protecting the annotations framework from cross-tree corruption.","triggerScenarios":"Calling TextAnchor.Contains(textPointer) with an ITextPointer whose TextContainer property differs from this.Start.TextContainer — e.g. a pointer obtained from a second FlowDocument/TextBox while the anchor belongs to the first.","commonSituations":"Annotation code that stores TextRange/anchor data across multiple documents or recreates a document instance and mixes old pointers with a new anchor; copy-paste logic that carries pointers between containers.","solutions":["Pass a pointer obtained from the same TextContainer as the anchor (anchor.Start.TextContainer).","If you have a pointer from another tree, resolve it back to a container-agnostic offset (or TextMap/annotation locator) and re-create a pointer inside the target container.","Guard the call: check textPointer.TextContainer == anchor.Start.TextContainer before calling Contains."],"exampleFix":"// before\nbool hit = anchor.Contains(pointerFromOtherDocument);\n// after\nif (pointer.TextContainer == anchor.Start.TextContainer)\n{\n    bool hit = anchor.Contains(pointer);\n}","handlingStrategy":"validation","validationCode":"if (pointer == null || pointer.TextContainer != anchor.Start.TextContainer)\n    throw new ArgumentException(\"textPointer belongs to a different TextContainer\");","typeGuard":"bool IsInAnchorTree(ITextPointer p, TextAnchor a) => p != null && p.TextContainer == a.Start.TextContainer;","tryCatchPattern":null,"preventionTips":["Always obtain pointers from the same document/container the anchor was created against.","Do not cache ITextPointer instances across document lifetime changes.","Centralize pointer creation in one helper that receives the TextContainer."],"tags":["wpf","argument","annotations","text-pointer"],"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"}