dotnet/wpf · error · ArgumentException

SR.Format(SR.TextSchema_ThisInlineUIContainerHasAChildUIElem…

Error message

SR.Format(SR.TextSchema_ThisInlineUIContainerHasAChildUIElementAlready, this.GetType().Name, ((InlineUIContainer)this).Child.GetType().Name, value.GetType().Name)

What it means

TextElement.AddChild for an InlineUIContainer throws ArgumentException when the container already has a Child UIElement. The WPF text schema allows only one child element per InlineUIContainer, so a second AddChild is rejected, naming the container type, existing child type, and new value type.

Solutions

  1. Clear the existing child (inlineContainer.Child = null or RemoveChild) before AddChild.
  2. Create a new InlineUIContainer for the new UIElement.
  3. Check the Child property before calling AddChild.

Example fix

// before
inlineContainer.AddChild(newButton); // already has an Image child
// after
if (inlineContainer.Child != null) inlineContainer.Child = null;
inlineContainer.AddChild(newButton);
Defensive patterns

Strategy: validation

Validate before calling

bool canAdd = !(element is InlineUIContainer ic) || ic.Child == null;
if (!canAdd) container.Child = null; // clear first

Try / catch

try { container.AddChild(uie); }
catch (ArgumentException) { container.Child = null; container.AddChild(uie); }

Prevention

When it happens

Trigger: Calling AddChild on an InlineUIContainer (or text element that resolves to one) whose Child property is already set, e.g. adding a second Image/Button to the same inline container.

Common situations: XAML parsing or code that reuses a container across data-binding updates without clearing the previous child.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextElement.cs:808

            TextElement te = value as TextElement;

            if (te != null)
            {
                TextSchema.ValidateChild(/*parent:*/this, /*child:*/te, true /* throwIfIllegalChild */, true /* throwIfIllegalHyperlinkDescendent */);
                Append(te);
            }
            else
            {
                UIElement uie = value as UIElement;
                if (uie != null)
                {
                    InlineUIContainer inlineContainer = this as InlineUIContainer;
                    if (inlineContainer != null)
                    {
                        if (inlineContainer.Child != null)
                        {
                            throw new ArgumentException(SR.Format(SR.TextSchema_ThisInlineUIContainerHasAChildUIElementAlready, this.GetType().Name, ((InlineUIContainer)this).Child.GetType().Name, value.GetType().Name));
                        }

                        inlineContainer.Child = uie;
                    }
                    else
                    {
                        BlockUIContainer blockContainer = this as BlockUIContainer;
                        if (blockContainer != null)
                        {
                            if (blockContainer.Child != null)
                            {
                                throw new ArgumentException(SR.Format(SR.TextSchema_ThisBlockUIContainerHasAChildUIElementAlready, this.GetType().Name, ((BlockUIContainer)this).Child.GetType().Name, value.GetType().Name));
                            }

                            blockContainer.Child = uie;
                        }
                        else
                        {

View on GitHub (pinned to 81131a70a4)