{"record":{"id":"05b5942f009641a8","repo":"dotnet/wpf","slug":"sr-format-sr-cannotchangeaftersealed-template","errorCode":null,"errorMessage":"SR.Format(SR.CannotChangeAfterSealed, \"Template\")","messagePattern":"SR\\.Format\\(SR\\.CannotChangeAfterSealed, \"Template\"\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkTemplate.cs","lineNumber":240,"sourceCode":"                        CanBeAccessedAcrossThreads = true\n                    };\n                }\n\n                if ( IsSealed )\n                {\n                    _resources.IsReadOnly = true;\n                }\n\n                return _resources;\n            }\n            set\n            {\n                // Verify Context Access\n                VerifyAccess();\n\n                if ( IsSealed )\n                {\n                    throw new InvalidOperationException(SR.Format(SR.CannotChangeAfterSealed, \"Template\"));\n                }\n\n                _resources = value;\n\n                // A Template ResourceDictionary can be accessed across threads\n                _resources?.CanBeAccessedAcrossThreads = true;\n            }\n        }\n\n        ResourceDictionary IHaveResources.Resources\n        {\n            get { return Resources; }\n            set { Resources = value; }\n        }\n\n        /// <summary>\n        ///     Tries to find a Reosurce for the given resourceKey in the current\n        ///     template's ResourceDictionary.","sourceCodeStart":222,"sourceCodeEnd":258,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkTemplate.cs#L222-L258","documentation":"The Resources setter on FrameworkTemplate throws InvalidOperationException (SR.CannotChangeAfterSealed, \"Template\") when the template is sealed. A template's resource dictionary is frozen once the template is applied, because instantiated template trees rely on those resources being stable.","triggerScenarios":"Assigning template.Resources after the template was sealed (applied to a control or sealed during XAML load/layout).","commonSituations":"Adding resources to a ControlTemplate at runtime after it is assigned to a control's Template property; editing application resources nested inside a loaded template; trying to inject theme resources after first render.","solutions":["Populate template.Resources in XAML or in code before the template is assigned/used.","Create a new template instance with the desired resources and assign it to the control's Template property.","Put runtime-changeable resources in a higher-scope dictionary (Window/App Resources) instead of the template."],"exampleFix":"// before\ntemplate.Resources.Add(\"brush\", brush); // throws if sealed\ncontrol.Template = template;\n// after\ntemplate.Resources.Add(\"brush\", brush); // before sealing\ncontrol.Template = template;","handlingStrategy":"validation","validationCode":"if (!template.IsSealed)\n    template.Resources[key] = value;\nelse\n    control.Template = BuildTemplateWithResource(key, value);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Populate template resources before assigning the template to a control.","Put dynamically changing resources in Window/App-level dictionaries."],"tags":["wpf","templates","resources","invalid-state-transition"],"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-22T01:17:13.364Z"}