{"record":{"id":"1fb0d31152678fbe","repo":"dotnet/wpf","slug":"sr-indifferenttextcontainers","errorCode":null,"errorMessage":"SR.InDifferentTextContainers","messagePattern":"SR\\.InDifferentTextContainers","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/ValidationHelper.cs","lineNumber":59,"sourceCode":"\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            {\n                throw new ArgumentException(SR.Format(SR.BadTextPositionOrder, \"startPosition\", \"endPosition\"));\n            }\n        }\n\n        // Throws an ArgumentException if direction is not a valid enum.\n        internal static void VerifyDirection(LogicalDirection direction, string argumentName)\n        {\n            if (direction != LogicalDirection.Forward &&\n                direction != LogicalDirection.Backward)\n            {\n                throw new InvalidEnumArgumentException(argumentName, (int)direction, typeof(LogicalDirection));\n            }\n        }\n\n        // Throws an ArgumentException if edge is not a valid enum.","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/ValidationHelper.cs#L41-L77","documentation":"ValidationHelper.VerifyPositionPair throws ArgumentException(SR.InDifferentTextContainers) when startPosition and endPosition come from different ITextContainers. A text span (start,end) must be defined within one document; WPF validates this up front.","triggerScenarios":"Calling span APIs (TextRange construction, selection setting, VerifyPositionPair) with start/end pointers originating from different documents/containers.","commonSituations":"Constructing TextRange(start, end) where one pointer was cached from a previous document; selection APIs fed with pointers from a second RichTextBox; copy/paste code mixing source and target pointers.","solutions":["Ensure both pointers come from the same TextContainer; re-acquire from the current document when in doubt.","Check startPosition.TextContainer == endPosition.TextContainer before building ranges.","Reset cached pointers whenever the Document property changes (listen to DocumentChanged/Loaded)."],"exampleFix":"// before\nvar range = new TextRange(oldDocStart, newDocEnd); // may throw\n// after\nif (oldDocStart.TextContainer != newDocEnd.TextContainer)\n    oldDocStart = currentDoc.ContentStart;\nvar range = new TextRange(oldDocStart, newDocEnd);","handlingStrategy":"type-guard","validationCode":"bool pairValid = startPosition != null && endPosition != null\n    && startPosition.TextContainer == endPosition.TextContainer\n    && startPosition.CompareTo(endPosition) <= 0;","typeGuard":"bool SameContainerOrdered(ITextPointer s, ITextPointer e) => s != null && e != null && s.TextContainer == e.TextContainer && s.CompareTo(e) <= 0;","tryCatchPattern":"try { var range = new TextRange(start, end); }\ncatch (ArgumentException) { start = doc.ContentStart; end = doc.ContentEnd; range = new TextRange(start, end); }","preventionTips":["Build ranges from pointers of one document only","Invalidate cached pointers on Document change","Validate start<=end and same container before span APIs"],"tags":["wpf","textpointer","argument","text-range"],"backgroundTag":"type-mismatch","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"}