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
- Use a factory type that implements IAddChild (e.g. ContentPresenter, TextBlock) to host children.
- Remove the AppendChild calls and configure properties on the child type differently.
- Implement IAddChild on your custom element type if children must be supported.
- 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
- Before AppendChild, confirm the factory's type implements IAddChild.
- Use known-safe containers (ContentPresenter, Decorator-derived, TextBlock) for children.
- Implement IAddChild on custom elements that must host factory children.
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
- SR.Format(SR.TypeMustImplementIAddChild…
- SR.Format(SR.TypeMustImplementIAddChild…
- Decorator marked as PART_ContentHost must have no content.
- InvalidOperationException (no message)
- Only Decorator and ScrollViewer can be used as…
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 providedView on GitHub (pinned to 81131a70a4)