{"record":{"id":"495a18cb787ce89a","repo":"dotnet/wpf","slug":"documentviewer-element-can-have-only-one-child","errorCode":null,"errorMessage":"DocumentViewer element can have only one child.","messagePattern":"DocumentViewer element can have only one child\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Primitives/DocumentViewerBase.cs","lineNumber":1701,"sourceCode":"        //\n        //  IAddChild\n        //\n        //-------------------------------------------------------------------\n\n        #region IAddChild\n\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);","sourceCodeStart":1683,"sourceCodeEnd":1719,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Primitives/DocumentViewerBase.cs#L1683-L1719","documentation":"DocumentViewerBase implements IAddChild so XAML parsers can add a document as its single child. Since a DocumentViewer can host exactly one FlowDocument-family document, calling IAddChild.AddChild when Document is already set throws InvalidOperationException.","triggerScenarios":"Declaring two document children inside a <DocumentViewer> in XAML, or calling AddChild on the viewer in code after Document (or a previous child) has been set.","commonSituations":"Copying a FlowDocument element inside a DocumentViewer block, or programmatically adding content without checking whether a document is already assigned.","solutions":["Remove the extra child so only one IDocumentPaginatorSource remains inside the viewer","Clear/replace the Document property instead of calling AddChild to change the document","Wrap multiple documents in separate viewers (e.g. FlowDocumentPageViewer per document) if more content is needed"],"exampleFix":"// before\n<DocumentViewer>\n  <FlowDocument/><FlowDocument/>\n</DocumentViewer>\n// after\n<DocumentViewer>\n  <FlowDocument/>\n</DocumentViewer>","handlingStrategy":"validation","validationCode":"if (viewer.Document != null) { /* replace instead of adding */ viewer.Document = newDoc; }","typeGuard":null,"tryCatchPattern":"try { ((IAddChild)viewer).AddChild(doc); }\ncatch (InvalidOperationException) { /* Document already set - assign Document instead */ }","preventionTips":["Give DocumentViewer exactly one document child in XAML","Replace documents via the Document property, not AddChild","Search for duplicated FlowDocument blocks inside DocumentViewer markup"],"tags":["wpf","documentviewer","xaml"],"backgroundTag":"invalid-state-transition","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}