{"record":{"id":"ad54133a6caf3ca4","repo":"dotnet/wpf","slug":"sr-format-sr-typemustimplementiaddchild-type-name","errorCode":null,"errorMessage":"SR.Format(SR.TypeMustImplementIAddChild, _type.Name)","messagePattern":"SR\\.Format\\(SR\\.TypeMustImplementIAddChild, _type\\.Name\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkElementFactory.cs","lineNumber":553,"sourceCode":"\n            // Perform actual Seal operation\n            Seal();\n        }\n\n        private void Seal()\n        {\n            if (_type == null && _text == null)\n            {\n                throw new InvalidOperationException(SR.NullTypeIllegal);\n            }\n\n            if (_firstChild != null)\n            {\n                // This factory has children, it must implement IAddChild so that these\n                // children can be added to the logical tree\n                if (!typeof(IAddChild).IsAssignableFrom(_type))\n                {\n                    throw new InvalidOperationException(SR.Format(SR.TypeMustImplementIAddChild, _type.Name));\n                }\n            }\n\n            ApplyAutoAliasRules();\n\n            if ((_childName != null) && (_childName != String.Empty))\n            {\n                // ChildName provided\n                if (!IsChildNameValid(_childName))\n                {\n                    throw new InvalidOperationException(SR.Format(SR.ChildNameNamePatternReserved, _childName));\n                }\n\n                _childName = String.Intern(_childName);\n            }\n            else\n            {\n                // ChildName not provided","sourceCodeStart":535,"sourceCodeEnd":571,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkElementFactory.cs#L535-L571","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nvar f = new FrameworkElementFactory(typeof(CustomControl)); // no IAddChild\nf.AppendChild(childFactory);\n// after\nvar f = new FrameworkElementFactory(typeof(ContentPresenter));\nf.AppendChild(childFactory);","handlingStrategy":"validation","validationCode":"if (hasChildren && !typeof(IAddChild).IsAssignableFrom(elementType))\n    throw new InvalidOperationException($\"{elementType.Name} must implement IAddChild to host factory children.\");","typeGuard":"static bool SupportsChildren(Type t) => typeof(IAddChild).IsAssignableFrom(t);","tryCatchPattern":"try { factory.AppendChild(child); }\ncatch { /* choose an IAddChild container */ factory = new FrameworkElementFactory(typeof(ContentPresenter)); factory.AppendChild(child); }","preventionTips":["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."],"tags":["wpf","iaddchild","template"],"backgroundTag":"unsupported-operation","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}