{"record":{"id":"ae699249a5770494","repo":"dotnet/wpf","slug":"sr-unexpectedparametertype-richtextbox","errorCode":null,"errorMessage":"SR.UnexpectedParameterType","messagePattern":"SR\\.UnexpectedParameterType","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/RichTextBox.cs","lineNumber":153,"sourceCode":"        // IAddChild interface\n        //\n        // -----------------------------------------------------------\n\n        ///<summary>\n        /// This method is called to Add the object as a child of the RichTextBox.  This method is used primarily\n        /// by the parser; a more direct way of adding a child to a RichTextBox is to use the <see cref=\"Document\" />\n        /// property.\n        ///</summary>\n        ///<param name=\"value\">\n        /// The object to add as a child; it must be a UIElement.\n        ///</param>\n        void IAddChild.AddChild(Object value)\n        {\n            ArgumentNullException.ThrowIfNull(value);\n\n            if (!(value is FlowDocument))\n            {\n                throw new ArgumentException(SR.Format(SR.UnexpectedParameterType, value.GetType(), typeof(FlowDocument)), nameof(value));\n            }\n\n            if (!_implicitDocument)\n            {\n                throw new ArgumentException(SR.Format(SR.CanOnlyHaveOneChild, this.GetType(), value.GetType()));\n            }\n\n            this.Document = (FlowDocument)value;\n        }\n\n        ///<summary>\n        /// This method is called by the parser when text appears under the tag in markup.\n        /// As RichTextBox do not support text, calling this method has no effect if the text\n        /// is all whitespace.  For non-whitespace text, throw an exception.\n        ///</summary>\n        ///<param name=\"text\">\n        /// Text to add as a child.\n        ///</param> ","sourceCodeStart":135,"sourceCodeEnd":171,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/RichTextBox.cs#L135-L171","documentation":"RichTextBox implements IAddChild, and its only valid child is a single FlowDocument. Calling AddChild (directly or via XAML) with any other object type throws ArgumentException(SR.UnexpectedParameterType) naming the received type and the expected FlowDocument type.","triggerScenarios":"IAddChild.AddChild(value) where value is not a FlowDocument — e.g. putting a Paragraph, TextBlock, or arbitrary UIElement as direct XAML content of a RichTextBox.","commonSituations":"Writing XAML like <RichTextBox><Paragraph/></RichTextBox> (Paragraph must go inside a FlowDocument); copying markup patterns from TextBox/TextBlock; programmatically calling AddChild with a control.","solutions":["Wrap flow content in a FlowDocument: <RichTextBox><FlowDocument><Paragraph>...</Paragraph></FlowDocument></RichTextBox>.","Set the Document property instead of AddChild when constructing in code.","If you want inline text controls, use a TextBox or TextBlock rather than RichTextBox."],"exampleFix":"// before\nrichTextBox.AddChild(new Paragraph(new Run(\"hi\")));\n// after\nrichTextBox.Document = new FlowDocument(new Paragraph(new Run(\"hi\")));","handlingStrategy":"type-guard","validationCode":"if (child is not FlowDocument)\n    throw new ArgumentException(\"RichTextBox child must be a FlowDocument\", nameof(child));\nrichTextBox.AddChild(child);","typeGuard":"bool IsFlowDocumentChild(object v) => v is FlowDocument;","tryCatchPattern":"try { richTextBox.AddChild(value); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"FlowDocument\")) { richTextBox.Document = new FlowDocument(); }","preventionTips":["In XAML always nest flow content inside <FlowDocument> under RichTextBox.","Prefer setting the Document property over IAddChild.AddChild in code.","Remember RichTextBox content model differs from TextBox: only a FlowDocument is accepted."],"tags":["wpf","richtextbox","xaml","argument-exception"],"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"}