dotnet/wpf · error · ArgumentException

SR.Format(SR.UnexpectedParameterType, value.GetType()…

Error message

SR.Format(SR.UnexpectedParameterType, value.GetType(), typeof(UIElement))

What it means

AdornedElementPlaceholder.AddChild throws this ArgumentException because the element's only valid child is a UIElement. The parser/IAddChild pipeline passes the added child here, and any other object type (string handled separately, arbitrary objects) is rejected.

Solutions

  1. Only place a UIElement (e.g. a Panel, Border, Control) as the child of AdornedElementPlaceholder in XAML.
  2. Wrap non-UIElement content in a container that hosts it (e.g. a TextBlock for text).
  3. Guard programmatic AddChild calls with `if (value is UIElement)`.
  4. Note null children are silently ignored, so no guard is needed for null.

Example fix

<!-- before -->
<AdornedElementPlaceholder Name="ph">Some text</AdornedElementPlaceholder>
<!-- after -->
<AdornedElementPlaceholder Name="ph">
  <TextBlock Text="Some text"/>
</AdornedElementPlaceholder>
Defensive patterns

Strategy: type-guard

Validate before calling

if (value != null && !(value is UIElement)) throw new ArgumentException("AdornedElementPlaceholder child must be a UIElement");

Type guard

bool IsValidChild(object o) => o == null || o is UIElement;

Try / catch

try { placeholder.AddChild(value); } catch (ArgumentException) { /* wrap value in a UIElement container */ }

Prevention

When it happens

Trigger: Adding a non-UIElement child via XAML content or IAddChild.AddChild (e.g. a raw string node routed to AddChild, or a non-visual object from markup extensions).

Common situations: Placing text or non-visual content directly inside <AdornedElementPlaceholder> in XAML; programmatic AddChild calls with wrong-typed objects.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/AdornedElementPlaceholder.cs:59

        #endregion Constructors


        ///<summary>
        /// This method is called to Add the object as a child.  This method is used primarily
        /// by the parser; a more direct way of adding a child is to use the <see cref="Child" />
        /// property.
        ///</summary>
        ///<param name="value">
        /// The object to add as a child; it must be a UIElement.
        ///</param>
        void IAddChild.AddChild (Object value)
        {
            // keeping consistent with other elements:  adding null is a no-op.
            if (value == null)
                return;

            if (!(value is UIElement))
                throw new ArgumentException (SR.Format(SR.UnexpectedParameterType, value.GetType(), typeof(UIElement)), nameof(value));

            if (this.Child != null)
                throw new ArgumentException(SR.Format(SR.CanOnlyHaveOneChild, this.GetType(), value.GetType()));

            this.Child = (UIElement)value;
        }

        ///<summary>
        /// This method is called by the parser when text appears under the tag in markup.
        /// Calling this method has no effect if text is just whitespace.  If text is not
        /// just whitespace, throw an exception.
        ///</summary>
        ///<param name="text">
        /// Text to add as a child.
        ///</param>
        void IAddChild.AddText (string text)
        {
            XamlSerializerUtil.ThrowIfNonWhiteSpaceInAddText(text, this);

View on GitHub (pinned to 81131a70a4)