dotnet/wpf · error · ArgumentException

SR.Format(SR.DocumentStructureUnexpectedParameterType4…

Error message

SR.Format(SR.DocumentStructureUnexpectedParameterType4, value.GetType(), typeof(ParagraphStructure), typeof(FigureStructure), typeof(ListStructure), typeof(TableStructure))

What it means

StoryFragment/SectionStructure AddChild (SemanticBasicElement) only accepts document-structure block elements: ParagraphStructure, FigureStructure, ListStructure or TableStructure. Any other object passed to IAddChild.AddChild is rejected with ArgumentException describing the value's actual type and the four allowed types.

Solutions

  1. Wrap the named run in a ParagraphStructure > NamedElement hierarchy before adding.
  2. Only add ParagraphStructure, FigureStructure, ListStructure, or TableStructure to this container.
  3. Use the strongly typed Add(NamedElement) method on the container that expects leaves.

Example fix

// before
section.AddChild(namedElement);
// after
var para = new ParagraphStructure();
para.AddChild(namedElement);
section.AddChild(para);
Defensive patterns

Strategy: type-guard

Validate before calling

if (value is not (ParagraphStructure or FigureStructure or ListStructure or TableStructure)) throw new ArgumentException($"{value.GetType()} is not a valid block structure child.");

Type guard

bool IsValidBlockChild(object v) => v is ParagraphStructure or FigureStructure or ListStructure or TableStructure;

Try / catch

try { section.AddChild(value); } catch (ArgumentException) { /* wrap value in ParagraphStructure or reject the input */ }

Prevention

When it happens

Trigger: Calling AddChild with a non-BlockElement such as NamedElement, a UIElement, or a string on a container expecting structural blocks (e.g. adding a NamedElement directly to a SectionStructure instead of wrapping it in a ParagraphStructure).

Common situations: XAML parsing of fixed-document structure where a wrong child element appears under a section; programmatic construction of a FixedDocument structure mixing NamedElement leaves into block containers.

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


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/DocumentStructures/SemanticBasicElement.cs:69

            _elementType = FixedElement.ElementType.Section;
        }

        public void Add(BlockElement element)
        {
            ArgumentNullException.ThrowIfNull(element);
            ((IAddChild) this).AddChild(element);
        }
        
        void IAddChild.AddChild(object value)
        {
            if (value is ParagraphStructure || value is FigureStructure
                || value is ListStructure || value is TableStructure )
            {
                _elementList.Add((BlockElement)value);
                return;
            }

            throw new ArgumentException(SR.Format(SR.DocumentStructureUnexpectedParameterType4, value.GetType(),
                typeof(ParagraphStructure), typeof(FigureStructure), typeof(ListStructure), typeof(TableStructure)), 
                nameof(value));
        }

        void IAddChild.AddText(string text) { }
        
        IEnumerator<BlockElement> IEnumerable<BlockElement>.GetEnumerator()
        {
            throw new NotSupportedException();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return ((IEnumerable<BlockElement>)this).GetEnumerator();
        }
    }

    /// <summary>

View on GitHub (pinned to 81131a70a4)