dotnet/wpf · error · ArgumentException

SR.TextSchema_ChildTypeIsInvalid

Error message

SR.TextSchema_ChildTypeIsInvalid

What it means

TextBlock's IAddChild.AddChild throws ArgumentException when the child's type is not valid for the parent element under the text schema. The parent (e.g. a Paragraph) only accepts Inline children; a UIElement is auto-wrapped in InlineUIContainer, but any other type (e.g. a string object, control, or wrong FlowDocument element) is rejected.

Solutions

  1. Wrap non-inline content in a valid Inline type: Run for text, InlineUIContainer for UIElements.
  2. Use the Text property for plain strings instead of AddChild.
  3. Check the text schema: only Inline (Run, Span, Bold, Italic, Hyperlink, LineBreak, InlineUIContainer) is accepted as child.

Example fix

// before
textBlock.AddChild(new Paragraph(new Run("hi")));
// after
textBlock.AddChild(new Run("hi")); // or textBlock.Text = "hi";
Defensive patterns

Strategy: type-guard

Validate before calling

bool isValidChild = value is Inline || value is UIElement;
if (!isValidChild) return; // or wrap first

Type guard

static Inline ToInline(object v) =>
    v is Inline i ? i : v is UIElement u ? new InlineUIContainer(u) : null;

Try / catch

try { textBlock.AddChild(child); }
catch (ArgumentException ex) { Log.Error("Invalid TextBlock child for parent schema", ex); }

Prevention

When it happens

Trigger: Calling AddChild (or XAML nesting) with a child that is neither Inline nor UIElement inside a TextBlock/Paragraph, e.g. adding a Button directly is wrapped, but adding a string object or another TextBlock is rejected by the schema validator.

Common situations: XAML mistakes like nesting block elements (Paragraph) directly inside TextBlock content; passing non-flow objects programmatically; porting FlowDocument XAML into TextBlock content.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/a9ca671b3e783435. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/TextBlock.cs:145

                throw new ArgumentException(SR.Format(SR.TextPanelIllegalParaTypeForIAddChild, "value", value.GetType()));
            }

            // Get parent of the text container. Note that it can be not a "this" TextBlock - in case
            // when a TextBlock is used as a storage for plain TextBox - as an owner of a text container.
            Type parentType = _complexContent.TextContainer.Parent.GetType();

            Type valueType = value.GetType();

            // Do implicit conversion to allowed inline type - if possible
            if (!TextSchema.IsValidChildOfContainer(parentType, /*childType*/valueType))
            {
                if (value is UIElement)
                {
                    value = new InlineUIContainer((UIElement)value);
                }
                else
                {
                    throw new ArgumentException(SR.Format(SR.TextSchema_ChildTypeIsInvalid, parentType.Name, valueType.Name));
                }
            }

            // Insert inline element into the text container
            Invariant.Assert(value is Inline, "Schema validation helper must guarantee that invalid element is not passed here");

            TextContainer textContainer = (TextContainer)_complexContent.TextContainer;

            textContainer.BeginChange();
            try
            {
                TextPointer endPosition = textContainer.End;
                textContainer.InsertElementInternal(endPosition, endPosition, (Inline)value);
            }
            finally
            {
                textContainer.EndChange();
            }

View on GitHub (pinned to 81131a70a4)