dotnet/wpf · error · ArgumentException

SR.TextPanelIllegalParaTypeForIAddChild

Error message

SR.TextPanelIllegalParaTypeForIAddChild

What it means

TextBlock's IAddChild.AddChild throws ArgumentException when the text container used for complex content is not a full TextContainer (e.g. the TextBlock only stores plain text for a TextBox). Such a container cannot accept child objects through the XAML IAddChild path.

Solutions

  1. Do not add children to the TextBlock inside a TextBox template; use TextBox APIs instead.
  2. Use a standalone TextBlock (with its own TextContainer) when inline content is needed.
  3. Set the Text property rather than adding child paragraphs when the TextBlock is plain-text backed.

Example fix

// before
textBoxVisualTextBlock.AddChild(new Run("hello"));
// after
textBlock.Text = "hello"; // or use a standalone TextBlock for inline children
Defensive patterns

Strategy: validation

Validate before calling

if (!textBlock.Inlines-null-check && isTextBoxBackedVisual) // don't AddChild on TextBox's inner TextBlock
    return;

Type guard

static bool AcceptsChildren(TextBlock tb) => !(tb is { Parent: TextBox }); // only standalone TextBlocks host complex content

Try / catch

try { textBlock.AddChild(child); }
catch (ArgumentException) { /* this TextBlock is plain-text storage; use Text property */ }

Prevention

When it happens

Trigger: Adding a child object to a TextBlock whose complex content was created without a TextContainer — typically a TextBlock acting as the plain-text storage of a TextBox — via XAML parsing or programmatic AddChild calls.

Common situations: XAML that places child elements inside a TextBox's inner TextBlock; programmatic AddChild on a TextBlock reused as TextBox storage; template scenarios where the visual TextBlock is not the intended content host.

Understand the failure class

Background: "is not a compatible type" / "cannot merge" errors: when a value's type doesn't match what the library requires — this error's family across 65 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/TextBlock.cs:127

        //-------------------------------------------------------------------

        #region IAddChild members

        ///<summary>
        /// Called to Add the object as a Child.
        ///</summary>
        ///<param name="value">
        /// Object to add as a child
        ///</param>
        void IAddChild.AddChild(Object value)
        {
            ArgumentNullException.ThrowIfNull(value);

            EnsureComplexContent();

            if (!(_complexContent.TextContainer is TextContainer))
            {
                throw new ArgumentException(SR.Format(SR.TextPanelIllegalParaTypeForIAddChild, "value", value.GetType()));
            }

            // Get parent of the text container. Note that it can be not a "this" TextBlock - in case
            // when a TextBlock is used as a storage for plain TextBox - as an owner of a text container.
            Type parentType = _complexContent.TextContainer.Parent.GetType();

            Type valueType = value.GetType();

            // Do implicit conversion to allowed inline type - if possible
            if (!TextSchema.IsValidChildOfContainer(parentType, /*childType*/valueType))
            {
                if (value is UIElement)
                {
                    value = new InlineUIContainer((UIElement)value);
                }
                else
                {
                    throw new ArgumentException(SR.Format(SR.TextSchema_ChildTypeIsInvalid, parentType.Name, valueType.Name));

View on GitHub (pinned to 81131a70a4)