{"record":{"id":"b1be0141e09862b8","repo":"dotnet/wpf","slug":"sr-b1be01","errorCode":null,"errorMessage":"SR.FlowDocumentScrollViewerDocumentBelongsToAnotherFlowDocumentScrollViewerAlready","messagePattern":"SR\\.FlowDocumentScrollViewerDocumentBelongsToAnotherFlowDocumentScrollViewerAlready","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/FlowDocumentScrollViewer.cs","lineNumber":1184,"sourceCode":"                    {\n                        BringIntoView(targetRect);\n                    }\n                }\n            }\n        }\n\n        /// <summary>\n        /// The Document has changed and needs to be updated.\n        /// </summary>\n        private void DocumentChanged(FlowDocument oldDocument, FlowDocument newDocument)\n        {\n            // Use TextSelection to determine whether the new document belongs to another\n            // control or not.\n            if (newDocument != null &&\n                newDocument.StructuralCache.TextContainer != null &&\n                newDocument.StructuralCache.TextContainer.TextSelection != null)\n            {\n                throw new ArgumentException(SR.FlowDocumentScrollViewerDocumentBelongsToAnotherFlowDocumentScrollViewerAlready);\n            }\n\n            // Cleanup state associated with the old document.\n            if (oldDocument != null)\n            {\n                // If Document was added to logical tree of FlowDocumentScrollViewer before, remove it.\n                if (_documentAsLogicalChild)\n                {\n                    RemoveLogicalChild(oldDocument);\n                }\n                // Remove the document from the ContentHost.\n                RenderScope?.Document = null;\n\n                oldDocument.ClearValue(PathNode.HiddenParentProperty);\n                oldDocument.StructuralCache.ClearUpdateInfo(true);\n            }\n\n            // If FlowDocumentScrollViewer was created through style, then do not modify","sourceCodeStart":1166,"sourceCodeEnd":1202,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/FlowDocumentScrollViewer.cs#L1166-L1202","documentation":"FlowDocumentScrollViewer throws this ArgumentException from its Document property change handler when the FlowDocument being assigned is already attached to another control's text selection — its StructuralCache.TextContainer has a TextSelection, indicating ownership by another viewer/editor (e.g. RichTextBox or another FlowDocumentScrollViewer). A FlowDocument instance can only be hosted by one control at a time.","triggerScenarios":"Assigning a Document instance that is currently the Document of another FlowDocumentScrollViewer, FlowDocumentPageViewer/Reader, or edited inside a RichTextBox.","commonSituations":"Sharing one FlowDocument between several viewers to 'sync' display; moving a document from a RichTextBox to a viewer without detaching; reusing a static/shared document instance across windows.","solutions":["Give each viewer its own FlowDocument instance (clone or construct a new document).","Before reassigning, detach the document from its current owner: set the other control's Document to null first.","If you need multiple views of the same content, load the content (e.g. XamlReader) into separate FlowDocument instances."],"exampleFix":"// before\nviewerA.Document = sharedDoc;\nviewerB.Document = sharedDoc; // throws\n\n// after\nviewerA.Document = sharedDoc;\nviewerB.Document = CloneFlowDocument(sharedDoc); // independent copy","handlingStrategy":"validation","validationCode":"static bool IsOwnedByOtherControl(FlowDocument doc) =>\n    doc?.StructuralCache?.TextContainer?.TextSelection != null;\nif (IsOwnedByOtherControl(newDoc))\n    throw new InvalidOperationException(\"Document belongs to another viewer; clone or detach first.\");","typeGuard":null,"tryCatchPattern":"try { viewer.Document = candidate; }\ncatch (ArgumentException) { viewer.Document = CloneFlowDocument(candidate); }","preventionTips":["Never share a single FlowDocument instance across multiple viewers/editors.","Set the previous owner's Document to null before reassigning a document.","Clone documents when multiple views of the same content are needed."],"tags":["wpf","flowdocumentscrollviewer","shared-resource","invalid-argument-value"],"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-22T01:17:13.364Z"}