{"record":{"id":"19d721e3c8021421","repo":"dotnet/wpf","slug":"sr-flowdocumentreadercanhaveonlyonechild","errorCode":null,"errorMessage":"SR.FlowDocumentReaderCanHaveOnlyOneChild","messagePattern":"SR\\.FlowDocumentReaderCanHaveOnlyOneChild","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/FlowDocumentReader.cs","lineNumber":1917,"sourceCode":"        //\n        //  IAddChild Members\n        //\n        //-------------------------------------------------------------------\n\n        #region IAddChild Members\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>FlowDocumentScrollViewer only supports a single child of type IDocumentPaginator.</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 ArgumentException(SR.FlowDocumentReaderCanHaveOnlyOneChild);\n            }\n            if (!(value is FlowDocument))\n            {\n                throw new ArgumentException(SR.Format(SR.UnexpectedParameterType, value.GetType(), typeof(FlowDocument)), nameof(value));\n            }\n            Document = value as FlowDocument;\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>FlowDocumentScrollViewer does not support Text children.</remarks>\n        void IAddChild.AddText(string text)\n        {\n            XamlSerializerUtil.ThrowIfNonWhiteSpaceInAddText(text, this);\n        }\n","sourceCodeStart":1899,"sourceCodeEnd":1935,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/FlowDocumentReader.cs#L1899-L1935","documentation":"FlowDocumentReader's explicit IAddChild.AddChild implementation throws this ArgumentException when a child is added in markup while Document has already been set. FlowDocumentReader accepts only one child, which must be a FlowDocument that becomes its Document.","triggerScenarios":"XAML/programmatic use of IAddChild.AddChild when FlowDocumentReader.Document (or a prior child) is already non-null, e.g. two children declared inside <FlowDocumentReader>.","commonSituations":"Accidentally nesting two FlowDocument elements or a FlowDocument plus another element inside the reader in XAML; code that reuses a reader instance and adds a second document child instead of assigning Document.","solutions":["Declare only one FlowDocument child inside FlowDocumentReader in XAML.","Replace an existing document by assigning reader.Document = newDoc instead of adding another child.","Remove leftover extra elements (panels, text) from inside the FlowDocumentReader tag; only a FlowDocument is allowed as the single child."],"exampleFix":"// before (XAML)\n<FlowDocumentReader>\n  <FlowDocument/><FlowDocument/> <!-- throws -->\n</FlowDocumentReader>\n\n// after\n<FlowDocumentReader>\n  <FlowDocument/>\n</FlowDocumentReader>","handlingStrategy":"validation","validationCode":"if (reader.Document != null)\n{\n    reader.Document = newDoc; // replace, do not add\n}\nelse\n{\n    reader.Document = newDoc;","typeGuard":"static bool IsSingleFlowDocumentChild(FlowDocumentReader r, object child) => r.Document == null && child is FlowDocument;","tryCatchPattern":"try { reader.AddChild(value); }\ncatch (ArgumentException) { reader.Document = value as FlowDocument; }","preventionTips":["Prefer assigning reader.Document over AddChild in code.","Audit XAML for duplicate children inside FlowDocumentReader tags."],"tags":["wpf","flowdocumentreader","xaml","invalid-argument-value"],"backgroundTag":"invalid-argument-value","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"}