dotnet/wpf · error · ArgumentException

SR.Format(SR.CanOnlyHaveOneChild, this.GetType()…

Error message

SR.Format(SR.CanOnlyHaveOneChild, this.GetType(), value.GetType())

What it means

AdornedElementPlaceholder.AddChild throws this ArgumentException because an AdornedElementPlaceholder (used in Validation.ErrorTemplate) can contain only one child element. A second child assignment when Child is already set is rejected.

Solutions

  1. Wrap multiple children in a single container (Grid/StackPanel/Border) as the one allowed child.
  2. Remove or clear the existing Child before adding a new one.
  3. Reorder XAML so only one direct child element exists.

Example fix

<!-- before -->
<AdornedElementPlaceholder>
  <TextBlock Text="!"/>
  <Border/><-- second child -->
</AdornedElementPlaceholder>
<!-- after -->
<AdornedElementPlaceholder>
  <StackPanel>
    <TextBlock Text="!"/>
    <Border/>
  </StackPanel>
</AdornedElementPlaceholder>
Defensive patterns

Strategy: validation

Validate before calling

if (placeholder.Child != null) throw new InvalidOperationException("AdornedElementPlaceholder already has a child");

Type guard

bool CanAddChild(AdornedElementPlaceholder p) => p.Child == null;

Try / catch

try { placeholder.AddChild(value); } catch (ArgumentException) { /* wrap multiple children in a single panel */ }

Prevention

When it happens

Trigger: Adding two elements as content of <AdornedElementPlaceholder> in XAML; calling AddChild twice programmatically without clearing Child.

Common situations: XAML like `<AdornedElementPlaceholder><TextBlock/><Border/></AdornedElementPlaceholder>` in a custom ErrorTemplate; appending a control in code when one already exists.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

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

        ///<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)