dotnet/wpf · error · ArgumentException

SR.Format(SR.TextSchema_ChildTypeIsInvalid…

Error message

SR.Format(SR.TextSchema_ChildTypeIsInvalid, this.GetType().Name, value.GetType().Name)

What it means

TextElement.AddChild throws ArgumentException when the text schema does not allow a UIElement child of the given type in this element. For inline contexts where an implicit InlineUIContainer cannot be created (or the parent type disallows it), the child type is rejected with TextSchema_ChildTypeIsInvalid naming this element type and the value type.

Solutions

  1. Place the UIElement in a schema-valid parent (e.g. InlineUIContainer for inline, BlockUIContainer for block).
  2. Verify the allowed child types of the target element before AddChild.
  3. Convert the child to a supported wrapper type explicitly.

Example fix

// before
run.AddChild(myGrid); // invalid child type for Run
// after
var container = new InlineUIContainer(myGrid);
paragraph.Inlines.Add(container);
Defensive patterns

Strategy: validation

Validate before calling

bool isValidChild = element is Paragraph || element is Span; // verify schema allows UIElement child here
if (!isValidChild) throw new InvalidOperationException("Place UIElement in InlineUIContainer");

Try / catch

try { element.AddChild(uie); }
catch (ArgumentException) { var c = new InlineUIContainer(uie); ((Paragraph)element).Inlines.Add(c); }

Prevention

When it happens

Trigger: Calling AddChild with a UIElement whose type is not permitted as a child of this TextElement per the text schema (e.g. a block-level UIElement into a Run/Paragraph inline position that can't host it).

Common situations: Programmatic FlowDocument construction placing UIElements in schema-invalid positions; XAML where a UIElement child is nested under an element that can't contain it.

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/b79617ac34c0228c. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextElement.cs:836

                            if (blockContainer.Child != null)
                            {
                                throw new ArgumentException(SR.Format(SR.TextSchema_ThisBlockUIContainerHasAChildUIElementAlready, this.GetType().Name, ((BlockUIContainer)this).Child.GetType().Name, value.GetType().Name));
                            }

                            blockContainer.Child = uie;
                        }
                        else
                        {
                            if (TextSchema.IsValidChild(/*parent:*/this, /*childType:*/typeof(InlineUIContainer)))
                            {
                                // Create implicit InlineUIContainer wrapper for this UIElement
                                InlineUIContainer implicitInlineUIContainer = Inline.CreateImplicitInlineUIContainer(this);
                                Append(implicitInlineUIContainer);
                                implicitInlineUIContainer.Child = uie;
                            }
                            else
                            {
                                throw new ArgumentException(SR.Format(SR.TextSchema_ChildTypeIsInvalid, this.GetType().Name, value.GetType().Name));
                            }
                        }
                    }
                }
                else
                {
                    throw new ArgumentException(SR.Format(SR.TextSchema_ChildTypeIsInvalid, this.GetType().Name, value.GetType().Name));
                }
            }
        }

        ///<summary>
        /// Called when text appears under the tag in markup
        ///</summary>
        ///<param name="text">
        /// Text to Add to this TextElement
        ///</param>
        /// <ExternalAPI/>

View on GitHub (pinned to 81131a70a4)