dotnet/wpf · error · ArgumentException

SR.RichTextBox_DocumentBelongsToAnotherRichTextBoxAlready

Error message

SR.RichTextBox_DocumentBelongsToAnotherRichTextBoxAlready

What it means

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.

Solutions

  1. Assign a fresh FlowDocument instance to each RichTextBox.
  2. To move a document, first set the old owner's Document to a new empty FlowDocument, then assign it to the new RichTextBox.
  3. If sharing content is required, share data via a view-model or clone the FlowDocument instead of the instance.

Example fix

// before
otherEditor.Document = editor.Document; // shared instance
// after
otherEditor.Document = CloneFlowDocument(editor.Document);
Defensive patterns

Strategy: validation

Validate before calling

bool IsOwnedByOtherEditor(FlowDocument doc, RichTextBox self) =>
    doc != null && doc.StructuralCache?.TextContainer?.TextSelection != null && !ReferenceEquals(doc, self.Document);
if (IsOwnedByOtherEditor(candidateDoc, richTextBox)) candidateDoc = CloneFlowDocument(candidateDoc);

Type guard

bool IsFreeDocument(FlowDocument doc) =>
    doc == null || doc.StructuralCache?.TextContainer?.TextSelection == null;

Try / catch

try { richTextBox.Document = candidate; }
catch (ArgumentException) { richTextBox.Document = CloneFlowDocument(candidate); }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "is not a compatible type" / "cannot merge" errors: when a value's type doesn't match what the library requires — this error's family across 65 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/4c9fffa3f0007bf4. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/RichTextBox.cs:360

        /// A Property representing a content of this RichTextBox
        /// </summary>
        public FlowDocument Document
        {
            get
            {
                Invariant.Assert(_document != null);
                return _document;
            }

            set
            {
                ArgumentNullException.ThrowIfNull(value);

                if (value != _document &&
                    value.StructuralCache != null && value.StructuralCache.TextContainer != null && 
                    value.StructuralCache.TextContainer.TextSelection != null)
                {
                    throw new ArgumentException(SR.RichTextBox_DocumentBelongsToAnotherRichTextBoxAlready);
                }

                if (_document != null && this.TextSelectionInternal.ChangeBlockLevel > 0)
                {
                    throw new InvalidOperationException(SR.RichTextBox_CantSetDocumentInsideChangeBlock);
                }

                if (value == _document)
                {
                    // Same document nothing to do.
                    return;
                }

                // Identify the case for the _document initialization
                bool initialSetting = _document == null;
                
                // Detach existing FlowDocument
                if (_document != null)

View on GitHub (pinned to 81131a70a4)