{"record":{"id":"4c9fffa3f0007bf4","repo":"dotnet/wpf","slug":"sr-richtextbox-documentbelongstoanotherrichtextboxalready","errorCode":null,"errorMessage":"SR.RichTextBox_DocumentBelongsToAnotherRichTextBoxAlready","messagePattern":"SR\\.RichTextBox_DocumentBelongsToAnotherRichTextBoxAlready","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/RichTextBox.cs","lineNumber":360,"sourceCode":"        /// A Property representing a content of this RichTextBox\n        /// </summary>\n        public FlowDocument Document\n        {\n            get\n            {\n                Invariant.Assert(_document != null);\n                return _document;\n            }\n\n            set\n            {\n                ArgumentNullException.ThrowIfNull(value);\n\n                if (value != _document &&\n                    value.StructuralCache != null && value.StructuralCache.TextContainer != null && \n                    value.StructuralCache.TextContainer.TextSelection != null)\n                {\n                    throw new ArgumentException(SR.RichTextBox_DocumentBelongsToAnotherRichTextBoxAlready);\n                }\n\n                if (_document != null && this.TextSelectionInternal.ChangeBlockLevel > 0)\n                {\n                    throw new InvalidOperationException(SR.RichTextBox_CantSetDocumentInsideChangeBlock);\n                }\n\n                if (value == _document)\n                {\n                    // Same document nothing to do.\n                    return;\n                }\n\n                // Identify the case for the _document initialization\n                bool initialSetting = _document == null;\n                \n                // Detach existing FlowDocument\n                if (_document != null)","sourceCodeStart":342,"sourceCodeEnd":378,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/RichTextBox.cs#L342-L378","documentation":"RichTextBox.Document rejects a FlowDocument that is already attached to another RichTextBox: if the document's StructuralCache has a TextSelection (created only when hosted by a RichTextBox) and it is not the current document, the setter throws ArgumentException(SR.RichTextBox_DocumentBelongsToAnotherRichTextBoxAlready). This prevents two editors from sharing one text container.","triggerScenarios":"Assigning doc1.Document = doc2.Document where doc2 is another live RichTextBox, e.g. sharing a document across tabs, cloning editors, or moving a document between controls on selection change.","commonSituations":"Tabbed editors reusing one FlowDocument instance; preview panes binding to an editor's Document; copy-paste of XAML resource dictionaries that reference a shared document.","solutions":["Assign a fresh FlowDocument instance to each RichTextBox.","To move a document, first set the old owner's Document to a new empty FlowDocument, then assign it to the new RichTextBox.","If sharing content is required, share data via a view-model or clone the FlowDocument instead of the instance."],"exampleFix":"// before\notherEditor.Document = editor.Document; // shared instance\n// after\notherEditor.Document = CloneFlowDocument(editor.Document);","handlingStrategy":"validation","validationCode":"bool IsOwnedByOtherEditor(FlowDocument doc, RichTextBox self) =>\n    doc != null && doc.StructuralCache?.TextContainer?.TextSelection != null && !ReferenceEquals(doc, self.Document);\nif (IsOwnedByOtherEditor(candidateDoc, richTextBox)) candidateDoc = CloneFlowDocument(candidateDoc);","typeGuard":"bool IsFreeDocument(FlowDocument doc) =>\n    doc == null || doc.StructuralCache?.TextContainer?.TextSelection == null;","tryCatchPattern":"try { richTextBox.Document = candidate; }\ncatch (ArgumentException) { richTextBox.Document = CloneFlowDocument(candidate); }","preventionTips":["Treat a live FlowDocument as owned by exactly one RichTextBox.","Detach first: set the old owner's Document to a new FlowDocument before reassigning.","Prefer cloning or sharing a view-model over sharing document instances."],"tags":["wpf","richtextbox","flowdocument","argument-exception"],"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"}