{"record":{"id":"627e7b19535639e6","repo":"dotnet/wpf","slug":"sr-frameworkelementfactorymustbesealed","errorCode":null,"errorMessage":"SR.FrameworkElementFactoryMustBeSealed","messagePattern":"SR\\.FrameworkElementFactoryMustBeSealed","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkElementFactory.cs","lineNumber":923,"sourceCode":"                    throw new InvalidOperationException(SR.Format(SR.TypeMustImplementIAddChild,\n                                                         parent.GetType().Name));\n                }\n\n                ((IAddChild)parent).AddChild(childFrameworkObject.DO);\n            }\n        }\n\n\n        // This tree is used to instantiate a tree, represented by this factory.\n        // It is instantiated as a normal tree without any template optimizations.\n        // This is used by designers to inspect a template.\n\n        internal FrameworkObject InstantiateUnoptimizedTree()\n        {\n\n            if (!_sealed)\n            {\n                throw new InvalidOperationException(SR.FrameworkElementFactoryMustBeSealed);\n            }\n\n            // Create the object.\n\n            FrameworkObject frameworkObject = new FrameworkObject(CreateDependencyObject());\n\n            // Mark the beginning of initialization\n\n            frameworkObject.BeginInit();\n\n            // Set values for this object, taking them from the shared values table.\n\n            ProvideValueServiceProvider provideValueServiceProvider = null;\n            FrameworkTemplate.SetTemplateParentValues( Name, frameworkObject.DO, _frameworkTemplate, ref provideValueServiceProvider );\n\n            // Get the first child\n\n            FrameworkElementFactory childFactory = _firstChild;","sourceCodeStart":905,"sourceCodeEnd":941,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkElementFactory.cs#L905-L941","documentation":"InstantiateUnoptimizedTree is an internal entry point that requires the factory to be sealed; Seal() finalizes the factory tree (validates names, applies auto-alias rules) and is irreversible. If the factory was never sealed, the framework refuses to instantiate it and throws InvalidOperationException(SR.FrameworkElementFactoryMustBeSealed).","triggerScenarios":"Internally calling InstantiateUnoptimizedTree (e.g. via template infrastructure or a host like DataTemplate/ControlTemplate instantiation) on a FrameworkElementFactory whose Seal() has not yet run — typically because the factory was constructed in code and never attached to a template or Seal() was not explicitly called.","commonSituations":"Building a FrameworkTemplate/DataTemplate programmatically with a factory but forgetting to seal before the template is requested; calling internal instantiation paths from custom framework plumbing; using the factory directly without ever assigning it to a Template property.","solutions":["Call Seal() on the FrameworkElementFactory (or on the owning Template) before instantiation.","Assign the factory to a DataTemplate/ControlTemplate (whose Seal will seal the factory) before using it.","Ensure your template-building helper code finishes all factory mutations before the template's LoadContent is invoked, since Seal freezes the tree."],"exampleFix":"// before\nvar template = new DataTemplate();\ntemplate.VisualTree = factory;\nUIElement e = (UIElement)template.LoadContent(); // factory not sealed\n// after\ntemplate.VisualTree = factory;\ntemplate.Seal();\nUIElement e = (UIElement)template.LoadContent();","handlingStrategy":"validation","validationCode":"// ensure the factory/template is sealed before instantiation\ntemplate.Seal(); // throws earlier with a clearer message if the tree is invalid","typeGuard":"null","tryCatchPattern":"try { template.LoadContent(); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"sealed\")) { template.Seal(); /* retry once */ }","preventionTips":["Always call Seal() (or assign the factory to a Template that gets sealed) before LoadContent","Never mutate a factory after sealing","Centralize template construction in a helper that seals as its last step"],"tags":["wpf","template","invalid-state-transition","lifecycle"],"backgroundTag":"invalid-state-transition","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"}