{"record":{"id":"0368dd5321c2af67","repo":"dotnet/wpf","slug":"sr-notinassociatedtree","errorCode":null,"errorMessage":"SR.NotInAssociatedTree","messagePattern":"SR\\.NotInAssociatedTree","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextRangeBase.cs","lineNumber":65,"sourceCode":"\n        //......................................................\n        //\n        // Selection Building\n        //\n        //......................................................\n\n        /// <summary>\n        /// </summary>\n        // need to accound for position.LogicalDirection. -- see TextSegment.Contains implementation\n        internal static bool Contains(ITextRange thisRange, ITextPointer textPointer)\n        {\n            NormalizeRange(thisRange);\n\n            ArgumentNullException.ThrowIfNull(textPointer);\n\n            if (textPointer.TextContainer != thisRange.Start.TextContainer)\n            {\n                throw new ArgumentException(SR.NotInAssociatedTree, nameof(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(thisRange.Start) < 0)\n            {\n                textPointer = textPointer.GetFormatNormalizedPosition(LogicalDirection.Forward);\n            }\n            else if (textPointer.CompareTo(thisRange.End) > 0)\n            {\n                textPointer = textPointer.GetFormatNormalizedPosition(LogicalDirection.Backward);\n            }\n\n            // Check if at least one segment contains this position.\n            for (int i = 0; i < thisRange._TextSegments.Count; i++)\n            {\n                if (thisRange._TextSegments[i].Contains(textPointer))","sourceCodeStart":47,"sourceCodeEnd":83,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextRangeBase.cs#L47-L83","documentation":"ITextRange.Contains throws ArgumentException when the supplied TextPointer belongs to a different TextContainer than the range itself. Ranges can only reason about positions within their own text tree; cross-tree pointers are meaningless. The message name SR.NotInAssociatedTree conveys the pointer is not in the range's associated tree.","triggerScenarios":"Calling Contains(textPointer) on a TextRange/TextSelection whose Start.TextContainer differs from textPointer.TextContainer — e.g. testing whether a selection contains a pointer from another RichTextBox or FlowDocument.","commonSituations":"Multiple RichTextBox/FlowDocument instances in one window; comparing caret positions across documents after a paste or drag-drop between editors; caching TextPointers from one document and testing against a range in another.","solutions":["Ensure the TextPointer comes from the same document/container as the range","Compare within the correct TextSelection of the same RichTextBox","If cross-document checks are needed, compare textual content or convert positions explicitly rather than passing pointers across trees"],"exampleFix":"// before\nbool has = otherDoc.Selection.Contains(pointerFromDocA);\n// after\nbool has = (pointerFromDocA.TextContainer == range.Start.TextContainer) && range.Contains(pointerFromDocA);","handlingStrategy":"validation","validationCode":"if (pointer == null || range.Start == null || pointer.TextContainer != range.Start.TextContainer) throw new ArgumentException(\"pointer from different tree\");","typeGuard":"bool SameTree(ITextRange r, TextPointer p) => r.Start != null && p.TextContainer == r.Start.TextContainer;","tryCatchPattern":"try { bool c = range.Contains(pointer); } catch (ArgumentException ex) { /* cross-document pointer */ }","preventionTips":["Track which document each TextPointer originated from","Never cache pointers across RichTextBox/FlowDocument instances"],"tags":["wpf","textpointer","argument-validation"],"backgroundTag":"incompatible-source-type","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"}