dotnet/wpf · error · ArgumentException

SR.FlowDocumentScrollViewerDocumentBelongsToAnotherFlowDocum…

Error message

SR.FlowDocumentScrollViewerDocumentBelongsToAnotherFlowDocumentScrollViewerAlready

What it means

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.

Solutions

  1. Give each viewer its own FlowDocument instance (clone or construct a new document).
  2. Before reassigning, detach the document from its current owner: set the other control's Document to null first.
  3. If you need multiple views of the same content, load the content (e.g. XamlReader) into separate FlowDocument instances.

Example fix

// before
viewerA.Document = sharedDoc;
viewerB.Document = sharedDoc; // throws

// after
viewerA.Document = sharedDoc;
viewerB.Document = CloneFlowDocument(sharedDoc); // independent copy
Defensive patterns

Strategy: validation

Validate before calling

static bool IsOwnedByOtherControl(FlowDocument doc) =>
    doc?.StructuralCache?.TextContainer?.TextSelection != null;
if (IsOwnedByOtherControl(newDoc))
    throw new InvalidOperationException("Document belongs to another viewer; clone or detach first.");

Try / catch

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

Prevention

When it happens

Trigger: Assigning a Document instance that is currently the Document of another FlowDocumentScrollViewer, FlowDocumentPageViewer/Reader, or edited inside a RichTextBox.

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

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/b1be0141e09862b8. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/FlowDocumentScrollViewer.cs:1184

                    {
                        BringIntoView(targetRect);
                    }
                }
            }
        }

        /// <summary>
        /// The Document has changed and needs to be updated.
        /// </summary>
        private void DocumentChanged(FlowDocument oldDocument, FlowDocument newDocument)
        {
            // Use TextSelection to determine whether the new document belongs to another
            // control or not.
            if (newDocument != null &&
                newDocument.StructuralCache.TextContainer != null &&
                newDocument.StructuralCache.TextContainer.TextSelection != null)
            {
                throw new ArgumentException(SR.FlowDocumentScrollViewerDocumentBelongsToAnotherFlowDocumentScrollViewerAlready);
            }

            // Cleanup state associated with the old document.
            if (oldDocument != null)
            {
                // If Document was added to logical tree of FlowDocumentScrollViewer before, remove it.
                if (_documentAsLogicalChild)
                {
                    RemoveLogicalChild(oldDocument);
                }
                // Remove the document from the ContentHost.
                RenderScope?.Document = null;

                oldDocument.ClearValue(PathNode.HiddenParentProperty);
                oldDocument.StructuralCache.ClearUpdateInfo(true);
            }

            // If FlowDocumentScrollViewer was created through style, then do not modify

View on GitHub (pinned to 81131a70a4)