dotnet/wpf · error · ArgumentException

SR.Format(SR.TextSchema_ThisBlockUIContainerHasAChildUIEleme…

Error message

SR.Format(SR.TextSchema_ThisBlockUIContainerHasAChildUIElementAlready, this.GetType().Name, ((BlockUIContainer)this).Child.GetType().Name, value.GetType().Name)

What it means

TextElement.AddChild for a BlockUIContainer throws ArgumentException when the block container already holds a Child UIElement. BlockUIContainer permits exactly one child, mirroring InlineUIContainer; the error names the container type, current child type, and attempted value type.

Solutions

  1. Clear blockContainer.Child before adding a new one.
  2. Use a fresh BlockUIContainer for each new UIElement.
  3. Check Child for null before calling AddChild.

Example fix

// before
blockContainer.AddChild(newGrid); // Child already set
// after
if (blockContainer.Child != null) blockContainer.Child = null;
blockContainer.AddChild(newGrid);
Defensive patterns

Strategy: validation

Validate before calling

bool canAdd = !(element is BlockUIContainer bc) || bc.Child == null;
if (!canAdd) blockContainer.Child = null;

Try / catch

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

Prevention

When it happens

Trigger: Calling AddChild on a BlockUIContainer that already has a Child, e.g. adding a second UIElement (video, chart) to the same block container in a FlowDocument.

Common situations: Repopulating a FlowDocument at runtime without clearing containers; data binding assigning new content to an existing container.

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

Appendix: source

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

                {
                    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
                        {
                            if (TextSchema.IsValidChild(/*parent:*/this, /*childType:*/typeof(InlineUIContainer)))
                            {
                                // Create implicit InlineUIContainer wrapper for this UIElement
                                InlineUIContainer implicitInlineUIContainer = Inline.CreateImplicitInlineUIContainer(this);
                                Append(implicitInlineUIContainer);
                                implicitInlineUIContainer.Child = uie;
                            }
                            else
                            {
                                throw new ArgumentException(SR.Format(SR.TextSchema_ChildTypeIsInvalid, this.GetType().Name, value.GetType().Name));
                            }
                        }

View on GitHub (pinned to 81131a70a4)