{"record":{"id":"f4d1c031cc099a94","repo":"dotnet/wpf","slug":"sr-unexpectedparametertype-flowdocumentreader","errorCode":null,"errorMessage":"SR.UnexpectedParameterType","messagePattern":"SR\\.UnexpectedParameterType","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/FlowDocumentReader.cs","lineNumber":1921,"sourceCode":"\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\n        #endregion IAddChild Members\n\n        //-------------------------------------------------------------------\n        //","sourceCodeStart":1903,"sourceCodeEnd":1939,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/FlowDocumentReader.cs#L1903-L1939","documentation":"FlowDocumentReader's IAddChild.AddChild throws this ArgumentException (formatted with SR.UnexpectedParameterType) when the value passed is not a FlowDocument. The single child of a FlowDocumentReader must be a FlowDocument; any other element or object type is rejected with the actual and expected type names in the message.","triggerScenarios":"Calling AddChild with a non-FlowDocument value, or placing an element other than FlowDocument as the direct child of <FlowDocumentReader> in XAML.","commonSituations":"Putting a StackPanel, Paragraph, or other control directly under FlowDocumentReader instead of wrapping content in a FlowDocument; code-generated markup adding wrong-typed children.","solutions":["Wrap content in a FlowDocument (e.g. FlowDocument containing Paragraphs) before making it the child of FlowDocumentReader.","If you want arbitrary UI content, use a different container control instead of FlowDocumentReader.","Type-check the value before calling AddChild programmatically: if (value is FlowDocument) reader.AddChild(value);"],"exampleFix":"// before\nreader.AddChild(new Paragraph(new Run(\"hi\"))); // throws\n\n// after\nvar doc = new FlowDocument(new Paragraph(new Run(\"hi\")));\nreader.AddChild(doc); // or reader.Document = doc;","handlingStrategy":"type-guard","validationCode":"if (value is not FlowDocument doc)\n    throw new ArgumentException($\"Expected FlowDocument, got {value?.GetType().Name}\");\nreader.Document = doc;","typeGuard":"static bool IsFlowDocument(object v) => v is FlowDocument;","tryCatchPattern":"try { reader.AddChild(value); }\ncatch (ArgumentException) { // value was not a FlowDocument; wrap or reject }","preventionTips":["Type-check with `is FlowDocument` before adding children.","In XAML, place only a FlowDocument as the direct child of FlowDocumentReader."],"tags":["wpf","flowdocumentreader","type-mismatch","xaml"],"backgroundTag":"type-mismatch","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"}