dotnet/wpf · error
The child of a DocumentViewer must implement the…
Error message
The child of a DocumentViewer must implement the IDocumentPaginatorSource interface.
What it means
The child passed to DocumentViewerBase via IAddChild.AddChild must implement IDocumentPaginatorSource (as FlowDocument-hosting sources do). Any other object type throws ArgumentException because the viewer can only paginate and render such sources.
Solutions
- Pass an object implementing IDocumentPaginatorSource (e.g. assign a FlowDocument via the Document property)
- Use a ContentControl/ScrollViewer-based layout if you need to host arbitrary visuals instead of a DocumentViewer
- Set the viewer's Document property in code rather than AddChild with a non-document object
Example fix
// before
documentViewer.AddChild(new StackPanel());
// after
documentViewer.Document = new FlowDocument(new Paragraph(new Run("Hello"))); Defensive patterns
Strategy: type-guard
Validate before calling
if (value is IDocumentPaginatorSource doc) { viewer.Document = doc; } else { /* route to another container */ } Type guard
static bool IsValidViewerChild(object o) => o is System.Windows.Documents.IDocumentPaginatorSource;
Try / catch
try { ((IAddChild)viewer).AddChild(value); }
catch (ArgumentException) { /* value is not an IDocumentPaginatorSource */ } Prevention
- Only nest FlowDocument-hosting sources inside DocumentViewer
- Set Document directly in code instead of AddChild
- Use other layout controls for non-document content
When it happens
Trigger: Placing an arbitrary element inside <DocumentViewer> in XAML (parsed through IAddChild), or calling documentViewer.AddChild(someControl) in code with a non-document object.
Common situations: XAML typos nesting a Grid/StackPanel directly in a DocumentViewer, or misunderstanding that DocumentViewer hosts documents, not arbitrary content.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- ' ' is not a Visual or Visual3D.
- 0x80070057
- Animation_AnimationTimelineTypeMismatch
- Cannot have empty collection of DocumentPageView objects.
- DocumentViewer element can have only one child.
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/adcbd3cfd6539a69.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Primitives/DocumentViewerBase.cs:1707
/// <summary>
/// Called to add the object as a Child.
/// </summary>
/// <param name="value">Object to add as a child.</param>
/// <remarks>DocumentViewerBase only supports a single child of type IDocumentPaginatorSource.</remarks>
void IAddChild.AddChild(Object value)
{
ArgumentNullException.ThrowIfNull(value);
// Check if Content has already been set.
if (this.Document != null)
{
throw new InvalidOperationException(SR.DocumentViewerCanHaveOnlyOneChild);
}
// Only IDocumentPaginatorSource is a valid content.
IDocumentPaginatorSource document = value as IDocumentPaginatorSource;
if (document == null)
{
throw new ArgumentException(SR.DocumentViewerChildMustImplementIDocumentPaginatorSource, nameof(value));
}
this.Document = document;
}
/// <summary>
/// Called when text appears under the tag in markup
/// </summary>
/// <param name="text">Text to add to the Object.</param>
/// <remarks>DocumentViewer does not support Text children.</remarks>
void IAddChild.AddText(string text)
{
XamlSerializerUtil.ThrowIfNonWhiteSpaceInAddText(text, this);
}
#endregion IAddChild
//-------------------------------------------------------------------
//View on GitHub (pinned to 81131a70a4)