{"record":{"id":"9390f4fddfcacdb0","repo":"dotnet/wpf","slug":"sr-format-sr-cannotchangeaftersealed-frameworkelementfactory","errorCode":null,"errorMessage":"SR.Format(SR.CannotChangeAfterSealed, \"FrameworkElementFactory\")","messagePattern":"SR\\.Format\\(SR\\.CannotChangeAfterSealed, \"FrameworkElementFactory\"\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkElementFactory.cs","lineNumber":72,"sourceCode":"        /// <param name=\"name\">Style identifier</param>\n        public FrameworkElementFactory(Type type, string name)\n        {\n            Type = type;\n            Name = name;\n        }\n\n\n        /// <summary>\n        ///     Type of object that the factory will produce\n        /// </summary>\n        public Type Type\n        {\n            get { return _type; }\n            set\n            {\n                if (_sealed)\n                {\n                    throw new InvalidOperationException(SR.Format(SR.CannotChangeAfterSealed, \"FrameworkElementFactory\"));\n                }\n\n                if (_text != null)\n                {\n                    throw new InvalidOperationException(SR.FrameworkElementFactoryCannotAddText);\n                }\n\n                if ( value != null ) // We allow null up until Seal\n                {\n                    // If non-null, must be derived from one of the supported types\n                    if (!typeof(FrameworkElement).IsAssignableFrom(value) &&\n                        !typeof(FrameworkContentElement).IsAssignableFrom(value) &&\n                        !typeof(Visual3D).IsAssignableFrom(value))\n                    {\n                        throw new ArgumentException(SR.Format(SR.MustBeFrameworkOr3DDerived, value.Name));\n                    }\n                }\n","sourceCodeStart":54,"sourceCodeEnd":90,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkElementFactory.cs#L54-L90","documentation":"FrameworkElementFactory becomes sealed (immutable) once it is used to create a template instance. Attempting to set the Type property afterwards throws InvalidOperationException(SR.CannotChangeAfterSealed, \"FrameworkElementFactory\"). All configuration must happen before the factory is compiled into a template and instantiated.","triggerScenarios":"Setting FactoryType (or other properties) on a FrameworkElementFactory after a template that uses it has been sealed — i.e. after the template was applied/compiled and instances created.","commonSituations":"Mutating a shared factory cached in a static field after first use; changing factory Type in code after assigning it to a DataTemplate/ControlTemplate; hot-swapping template definitions at runtime without creating fresh factories.","solutions":["Create a new FrameworkElementFactory for each template (re)definition instead of reusing a sealed one","Perform all factory configuration (Type, properties, children) immediately after construction, before attaching to a template","Cache the DataTemplate, not the mutable factory, and rebuild factories when configuration changes","Check factory.IsSealed before mutating to fail fast in debug builds"],"exampleFix":"// before\nstatic FrameworkElementFactory factory = Build();\n// later\nfactory.FactoryType = typeof(Button); // throws when sealed\n// after\nvar factory = new FrameworkElementFactory(typeof(Button)); // fresh instance per template\ntemplate.VisualTree = factory;","handlingStrategy":"validation","validationCode":"if (factory.IsSealed) throw new InvalidOperationException(\"FrameworkElementFactory is sealed; create a new one\");","typeGuard":"bool CanMutateFactory(FrameworkElementFactory f) => !f.IsSealed;","tryCatchPattern":"try { factory.FactoryType = typeof(Button); }\ncatch (InvalidOperationException) { factory = new FrameworkElementFactory(typeof(Button)); }","preventionTips":["Treat factories as build-once, use-once","Never cache shared FrameworkElementFactory instances in static fields","Cache the DataTemplate and rebuild factories on config change"],"tags":["wpf","templates","immutable","invalid-operation"],"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"}