dotnet/wpf · error · InvalidOperationException

SR.Format(SR.TypeMustImplementIAddChild, _type.Name)

Error message

SR.Format(SR.TypeMustImplementIAddChild, _type.Name)

What it means

The private Seal throws InvalidOperationException when the factory has children but its target type does not implement IAddChild. Children added via AppendChild are attached through the IAddChild contract at instantiation time, so a factory whose type lacks IAddChild cannot host children. The message names the offending type.

Solutions

  1. Use a factory type that implements IAddChild (e.g. ContentPresenter, TextBlock) to host children.
  2. Remove the AppendChild calls and configure properties on the child type differently.
  3. Implement IAddChild on your custom element type if children must be supported.
  4. Restructure the visual tree so children belong to a container that supports them.

Example fix

// before
var f = new FrameworkElementFactory(typeof(CustomControl)); // no IAddChild
f.AppendChild(childFactory);
// after
var f = new FrameworkElementFactory(typeof(ContentPresenter));
f.AppendChild(childFactory);
Defensive patterns

Strategy: validation

Validate before calling

if (hasChildren && !typeof(IAddChild).IsAssignableFrom(elementType))
    throw new InvalidOperationException($"{elementType.Name} must implement IAddChild to host factory children.");

Type guard

static bool SupportsChildren(Type t) => typeof(IAddChild).IsAssignableFrom(t);

Try / catch

try { factory.AppendChild(child); }
catch { /* choose an IAddChild container */ factory = new FrameworkElementFactory(typeof(ContentPresenter)); factory.AppendChild(child); }

Prevention

When it happens

Trigger: Calling factory.AppendChild(childFactory) (leaving _firstChild set) on a factory whose _type does not implement IAddChild, then sealing — e.g. appending children to a Border-only or custom element type that lacks IAddChild support.

Common situations: Using types like Panels that support children in XAML via specialized paths but whose factory instantiation relies on IAddChild; custom controls without IAddChild receiving AppendChild calls; misremembering which types accept child factories.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkElementFactory.cs:553

            // Perform actual Seal operation
            Seal();
        }

        private void Seal()
        {
            if (_type == null && _text == null)
            {
                throw new InvalidOperationException(SR.NullTypeIllegal);
            }

            if (_firstChild != null)
            {
                // This factory has children, it must implement IAddChild so that these
                // children can be added to the logical tree
                if (!typeof(IAddChild).IsAssignableFrom(_type))
                {
                    throw new InvalidOperationException(SR.Format(SR.TypeMustImplementIAddChild, _type.Name));
                }
            }

            ApplyAutoAliasRules();

            if ((_childName != null) && (_childName != String.Empty))
            {
                // ChildName provided
                if (!IsChildNameValid(_childName))
                {
                    throw new InvalidOperationException(SR.Format(SR.ChildNameNamePatternReserved, _childName));
                }

                _childName = String.Intern(_childName);
            }
            else
            {
                // ChildName not provided

View on GitHub (pinned to 81131a70a4)