{"record":{"id":"f91474d1e05495a5","repo":"dotnet/wpf","slug":"sr-format-sr-notinassociatedcontainer-position","errorCode":null,"errorMessage":"SR.Format(SR.NotInAssociatedContainer, \"position\")","messagePattern":"SR\\.Format\\(SR\\.NotInAssociatedContainer, \"position\"\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/DocumentSequenceTextContainer.cs","lineNumber":346,"sourceCode":"        //\n        // Internal Methods\n        //\n        //---------------------------------------------------------------------\n\n        #region Internal Methods\n\n        //--------------------------------------------------------------------\n        // Utility Method\n        //---------------------------------------------------------------------\n\n        // Verify parameter. Throw Exception if necessary.\n        internal DocumentSequenceTextPointer VerifyPosition(ITextPointer position)\n        {\n            ArgumentNullException.ThrowIfNull(position);\n\n            if (position.TextContainer != this)\n            {\n                throw new ArgumentException(SR.Format(SR.NotInAssociatedContainer, \"position\"));\n            }\n\n            DocumentSequenceTextPointer tp = position as DocumentSequenceTextPointer;\n            if (tp == null)\n            {\n                throw new ArgumentException(SR.Format(SR.BadFixedTextPosition, \"position\"));\n            }\n\n            return tp;\n        }\n\n\n        // Given an ITextPointer in a child TextContainer, create a position in parent's\n        // address space to represent it.\n        internal DocumentSequenceTextPointer MapChildPositionToParent(ITextPointer tp)\n        {\n            ChildDocumentBlock cdb = this._doclistHead;\n            while (cdb != null)","sourceCodeStart":328,"sourceCodeEnd":364,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/DocumentSequenceTextContainer.cs#L328-L364","documentation":"DocumentSequenceTextContainer.VerifyPosition validates that a given ITextPointer belongs to this container. If position.TextContainer is a different container, it throws ArgumentException with NotInAssociatedContainer. This is an argument-ownership check before downcasting to DocumentSequenceTextPointer.","triggerScenarios":"Passing a TextPointer created by another document's TextContainer (e.g., a FlowDocument's TextPointer or another DocumentSequence's pointer) into APIs like GetOffsetToPosition, CompareTo, or internal move/verify paths on a DocumentSequenceTextContainer.","commonSituations":"Mixing pointers across documents when copying content between a FlowDocument and a FixedDocumentSequence; caching a pointer from a previous document instance and reusing it after the document changed.","solutions":["Ensure the pointer was created from the same DocumentSequenceTextContainer (or its Start/End) before passing it","Obtain pointers via documentSequence.TextContainer.Start.CreatePointer(...) rather than from other documents","Wrap position-taking calls in try/catch ArgumentException and re-acquire a fresh pointer"],"exampleFix":"// before\nseqContainer.VerifyPosition(pointerFromOtherDoc);\n// after\nif (pointer.TextContainer == seqContainer) { seqContainer.VerifyPosition(pointer); }","handlingStrategy":"validation","validationCode":"if (position == null || position.TextContainer != seqTextContainer)\n    throw new ArgumentException(\"position does not belong to this DocumentSequenceTextContainer\");","typeGuard":"bool BelongsToContainer(ITextPointer p, DocumentSequenceTextContainer c) => p != null && p.TextContainer == c;","tryCatchPattern":"try { seqContainer.SomePositionApi(pointer); }\ncatch (ArgumentException) { pointer = ((ITextContainer)seqTextContainer).Start.CreatePointer(); }","preventionTips":["Always create pointers from the same container they are used with","Don't cache pointers across document instances","Assert position.TextContainer == targetContainer in debug builds"],"tags":["wpf","documents","argument","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-22T01:17:13.364Z"}