{"record":{"id":"adcbd3cfd6539a69","repo":"dotnet/wpf","slug":"the-child-of-a-documentviewer-must-implement-the","errorCode":null,"errorMessage":"The child of a DocumentViewer must implement the IDocumentPaginatorSource interface.","messagePattern":"The child of a DocumentViewer must implement the IDocumentPaginatorSource interface\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Primitives/DocumentViewerBase.cs","lineNumber":1707,"sourceCode":"\n        /// <summary>\n        /// Called to add the object as a Child.\n        /// </summary>\n        /// <param name=\"value\">Object to add as a child.</param>\n        /// <remarks>DocumentViewerBase only supports a single child of type IDocumentPaginatorSource.</remarks>\n        void IAddChild.AddChild(Object value)\n        {\n            ArgumentNullException.ThrowIfNull(value);\n            // Check if Content has already been set.\n            if (this.Document != null)\n            {\n                throw new InvalidOperationException(SR.DocumentViewerCanHaveOnlyOneChild);\n            }\n            // Only IDocumentPaginatorSource is a valid content.\n            IDocumentPaginatorSource document = value as IDocumentPaginatorSource;\n            if (document == null)\n            {\n                throw new ArgumentException(SR.DocumentViewerChildMustImplementIDocumentPaginatorSource, nameof(value));\n            }\n            this.Document = document;\n        }\n\n        /// <summary>\n        /// Called when text appears under the tag in markup\n        /// </summary>\n        /// <param name=\"text\">Text to add to the Object.</param>\n        /// <remarks>DocumentViewer does not support Text children.</remarks>\n        void IAddChild.AddText(string text)\n        {\n            XamlSerializerUtil.ThrowIfNonWhiteSpaceInAddText(text, this);\n        }\n\n        #endregion IAddChild\n\n        //-------------------------------------------------------------------\n        //","sourceCodeStart":1689,"sourceCodeEnd":1725,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Primitives/DocumentViewerBase.cs#L1689-L1725","documentation":"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.","triggerScenarios":"Placing an arbitrary element inside <DocumentViewer> in XAML (parsed through IAddChild), or calling documentViewer.AddChild(someControl) in code with a non-document object.","commonSituations":"XAML typos nesting a Grid/StackPanel directly in a DocumentViewer, or misunderstanding that DocumentViewer hosts documents, not arbitrary content.","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"],"exampleFix":"// before\ndocumentViewer.AddChild(new StackPanel());\n// after\ndocumentViewer.Document = new FlowDocument(new Paragraph(new Run(\"Hello\")));","handlingStrategy":"type-guard","validationCode":"if (value is IDocumentPaginatorSource doc) { viewer.Document = doc; } else { /* route to another container */ }","typeGuard":"static bool IsValidViewerChild(object o) => o is System.Windows.Documents.IDocumentPaginatorSource;","tryCatchPattern":"try { ((IAddChild)viewer).AddChild(value); }\ncatch (ArgumentException) { /* value is not an IDocumentPaginatorSource */ }","preventionTips":["Only nest FlowDocument-hosting sources inside DocumentViewer","Set Document directly in code instead of AddChild","Use other layout controls for non-document content"],"tags":["wpf","documentviewer","type-mismatch"],"backgroundTag":"type-mismatch","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"}