dotnet/wpf · error · ArgumentException

SR.TextSchema_ChildTypeIsInvalid

Error message

SR.TextSchema_ChildTypeIsInvalid

What it means

ValidationHelper.ValidateChild throws ArgumentException(SR.TextSchema_ChildTypeIsInvalid) when TextSchema.IsValidChild says the child's type is not permitted at the given TextPointer position. The WPF text schema defines which element types may appear as children of each container type (e.g. only certain elements under Table/TableCell/Paragraph).

Solutions

  1. Wrap or insert the child in a schema-legal intermediate element (e.g. put content in a Paragraph, paragraphs in a Section).
  2. Insert at a position whose Parent permits the child type.
  3. Query TextSchema.IsValidChild in advance and choose the child type accordingly.

Example fix

// before
tableCell.Child = new Paragraph(...); // invalid
// after
var cellContent = new TableCell(new Paragraph(new Run("text")));
Defensive patterns

Strategy: validation

Validate before calling

if (!TextSchema.IsValidChild(position, child.GetType())) throw new ArgumentException($"{child.GetType().Name} not allowed under {position.Parent.GetType().Name}");

Try / catch

try { ... } catch (ArgumentException ex) { /* wrap child in a legal container or skip */ }

Prevention

When it happens

Trigger: Inserting a child whose runtime type is schema-invalid at the position — e.g. adding a Paragraph directly under a TableCell, or a TextElement under a container expecting UIElement content.

Common situations: Building flow documents programmatically and mixing up the allowed hierarchy; auto-generated code inserting elements at the wrong nesting level.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/ValidationHelper.cs:107

        // ...............................................................
        //
        // TextSchema Validation
        //
        // ...............................................................

        // Checks whether it is valid to insert the child object at passed position.
        internal static void ValidateChild(TextPointer position, object child, string paramName)
        {
            Invariant.Assert(position != null);

            if (child == null)
            {
                throw new ArgumentNullException(paramName);
            }

            if (!TextSchema.IsValidChild(/*position:*/position, /*childType:*/child.GetType()))
            {
                throw new ArgumentException(SR.Format(SR.TextSchema_ChildTypeIsInvalid, position.Parent.GetType().Name, child.GetType().Name));
            }

            // The new child should not be currently in other text tree
            if (child is TextElement)
            {
                if (((TextElement)child).Parent != null)
                {
                    throw new ArgumentException(SR.Format(SR.TextSchema_TheChildElementBelongsToAnotherTreeAlready, child.GetType().Name));
                }
            }
            else
            {
                Invariant.Assert(child is UIElement);
                // Cannot call UIElement.Parent across assembly boundary. So skip this part of validation. This condition will be checked elsewhere anyway.
                //if (((UIElement)child).Parent != null)
                //{
                //    throw new ArgumentException(SR.Format(SR.TextSchema_TheChildElementBelongsToAnotherTreeAlready, child.GetType().Name));
                //}

View on GitHub (pinned to 81131a70a4)