dotnet/wpf · error · ArgumentException

SR.Format(SR.DocumentStructureUnexpectedParameterType1…

Error message

SR.Format(SR.DocumentStructureUnexpectedParameterType1, value.GetType(), typeof(NamedElement))

What it means

This AddChild overload (on ParagraphStructure-style containers) accepts only NamedElement children; anything else triggers ArgumentException with DocumentStructureUnexpectedParameterType1, reporting the value's actual type and the single accepted type NamedElement. It enforces the fixed schema of WPF document structures.

Solutions

  1. Only add NamedElement instances (e.g. named runs) to this container.
  2. Move structural elements (Paragraph/List/Table) to the parent SectionStructure instead.
  3. Use IAddChild.AddText for plain text if the target supports it.

Example fix

// before
paragraphStructure.AddChild(new FigureStructure());
// after
paragraphStructure.AddChild(new NamedElement("x:P"));
Defensive patterns

Strategy: type-guard

Validate before calling

if (value is not NamedElement) throw new ArgumentException($"{value.GetType()} is not a NamedElement.");

Type guard

bool IsNamedElement(object v) => v is NamedElement;

Try / catch

try { container.AddChild(value); } catch (ArgumentException) { /* route non-NamedElement to the proper parent container */ }

Prevention

When it happens

Trigger: Passing a ParagraphStructure, TableStructure, raw string or arbitrary object to IAddChild.AddChild on a container whose legal child is only NamedElement.

Common situations: XAML in fixed documents where a <ParagraphStructure> is nested where only a <NamedElement> (e.g. <x:P> run mapping) is allowed; hand-built document trees copying the wrong nesting pattern.

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

Appendix: source

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

        {
            _elementType = FixedElement.ElementType.Paragraph;
        }

        public void Add(NamedElement element)
        {
            ArgumentNullException.ThrowIfNull(element);
            ((IAddChild) this).AddChild(element);
        }
        
        void IAddChild.AddChild(object value)
        {
            if (value is NamedElement)
            {
                _elementList.Add((BlockElement)value);
                return;
            }

            throw new ArgumentException(SR.Format(SR.DocumentStructureUnexpectedParameterType1, value.GetType(),
                typeof(NamedElement)),
                nameof(value));
        }
        
        void IAddChild.AddText(string text) { }

        IEnumerator<NamedElement> IEnumerable<NamedElement>.GetEnumerator()
        {
            throw new NotSupportedException();
        }

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

    /// <summary>

View on GitHub (pinned to 81131a70a4)