dotnet/wpf · error · ArgumentException
SR.UnexpectedParameterType
Error message
SR.UnexpectedParameterType
What it means
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.
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.
Example fix
// before
richTextBox.AddChild(new Paragraph(new Run("hi")));
// after
richTextBox.Document = new FlowDocument(new Paragraph(new Run("hi"))); Defensive patterns
Strategy: type-guard
Validate before calling
if (child is not FlowDocument)
throw new ArgumentException("RichTextBox child must be a FlowDocument", nameof(child));
richTextBox.AddChild(child); Type guard
bool IsFlowDocumentChild(object v) => v is FlowDocument;
Try / catch
try { richTextBox.AddChild(value); }
catch (ArgumentException ex) when (ex.Message.Contains("FlowDocument")) { richTextBox.Document = new FlowDocument(); } Prevention
- 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.
When it happens
Trigger: 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.
Common situations: Writing XAML like <RichTextBox><Paragraph/></RichTextBox> (Paragraph must go inside a FlowDocument); copying markup patterns from TextBox/TextBlock; programmatically calling AddChild with a control.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- SR.CanOnlyHaveOneChild
- SR.CanOnlyHaveOneChild
- SR.Format(SR.ParserPrefixNSProperty, nsPrefix, nameString)
- SR.NameScopeNameNotFound
- SR.ParserAttributeArgsLow
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/ae699249a5770494.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/RichTextBox.cs:153
// IAddChild interface
//
// -----------------------------------------------------------
///<summary>
/// This method is called to Add the object as a child of the RichTextBox. This method is used primarily
/// by the parser; a more direct way of adding a child to a RichTextBox is to use the <see cref="Document" />
/// property.
///</summary>
///<param name="value">
/// The object to add as a child; it must be a UIElement.
///</param>
void IAddChild.AddChild(Object value)
{
ArgumentNullException.ThrowIfNull(value);
if (!(value is FlowDocument))
{
throw new ArgumentException(SR.Format(SR.UnexpectedParameterType, value.GetType(), typeof(FlowDocument)), nameof(value));
}
if (!_implicitDocument)
{
throw new ArgumentException(SR.Format(SR.CanOnlyHaveOneChild, this.GetType(), value.GetType()));
}
this.Document = (FlowDocument)value;
}
///<summary>
/// This method is called by the parser when text appears under the tag in markup.
/// As RichTextBox do not support text, calling this method has no effect if the text
/// is all whitespace. For non-whitespace text, throw an exception.
///</summary>
///<param name="text">
/// Text to add as a child.
///</param> View on GitHub (pinned to 81131a70a4)