{"record":{"id":"84b3a6e9acce3987","repo":"dotnet/wpf","slug":"sr-notinassociatedcontainer","errorCode":null,"errorMessage":"SR.NotInAssociatedContainer","messagePattern":"SR\\.NotInAssociatedContainer","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/FixedTextContainer.cs","lineNumber":313,"sourceCode":"\n        //--------------------------------------------------------------------\n        //\n        // Internal Methods\n        //\n        //---------------------------------------------------------------------\n\n        #region Internal Methods\n\n        //--------------------------------------------------------------------\n        // Utility Method\n        //---------------------------------------------------------------------\n        internal FixedTextPointer 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            FixedTextPointer ftp = position as FixedTextPointer;\n\n            if (ftp == null)\n            {\n                throw new ArgumentException(SR.Format(SR.BadFixedTextPosition, \"position\"));\n            }\n\n            return ftp;\n        }\n\n\n        internal int GetPageNumber(ITextPointer  textPointer)\n        {\n            FixedTextPointer fixedTextPointer = textPointer as FixedTextPointer;\n            int  pageNumber = int.MaxValue;\n","sourceCodeStart":295,"sourceCodeEnd":331,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/FixedTextContainer.cs#L295-L331","documentation":"FixedTextContainer.VerifyPosition validates that an ITextPointer argument belongs to this FixedTextContainer. If position.TextContainer is a different container, an ArgumentException(SR.NotInAssociatedContainer) is thrown — the pointer is not associated with the container the API operates on.","triggerScenarios":"Calling a FixedTextContainer/FixedDocument API (e.g. text editor methods, VerifyPosition) passing an ITextPointer obtained from a different document or container.","commonSituations":"Mixing text pointers across two FixedDocument/FixedPage instances; caching a pointer from a document that was reloaded; passing a TextPointer from a FlowDocument into a FixedDocument API.","solutions":["Obtain the pointer from the same FixedTextContainer you pass it to (e.g. container.CreatePointerAtOffset).","Track which document/container each pointer came from; do not mix pointers between documents.","Re-create the pointer after reloading or rebuilding the document.","Catch ArgumentException and create a fresh pointer within the target container."],"exampleFix":"// before\nvar ftp = otherDocument.TextContainer.CreatePointerAtOffset(0, direction);\ntargetContainer.VerifyPosition(ftp); // throws\n// after\nvar ftp = targetContainer.CreatePointerAtOffset(0, direction);\ntargetContainer.VerifyPosition(ftp);","handlingStrategy":"type-guard","validationCode":"bool IsFromContainer(ITextPointer p, FixedTextContainer c) => p != null && p.TextContainer == c;","typeGuard":"bool CanVerify(ITextPointer p, FixedTextContainer c) => p?.TextContainer == c;","tryCatchPattern":"try { container.VerifyPosition(pointer); }\ncatch (ArgumentException) { pointer = container.CreatePointerAtOffset(0, LogicalDirection.Forward); }","preventionTips":["Never share text pointers across FixedDocument/FixedTextContainer instances.","Encapsulate pointer creation with its owning container.","Recreate pointers after documents are rebuilt or reloaded."],"tags":["wpf","documents","argument-exception","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"}