{"record":{"id":"d2366bd8fd0aa55c","repo":"dotnet/wpf","slug":"sr-format-sr-cannotchangeaftersealed-setterbase","errorCode":null,"errorMessage":"SR.Format(SR.CannotChangeAfterSealed, \"SetterBase\")","messagePattern":"SR\\.Format\\(SR\\.CannotChangeAfterSealed, \"SetterBase\"\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/SetterBase.cs","lineNumber":50,"sourceCode":"            get\n            {\n                return _sealed;\n            }\n        }\n\n        internal virtual void Seal()\n        {\n            _sealed = true;\n        }\n\n        /// <summary>\n        ///  Subclasses need to call this method before any changes to their state.\n        /// </summary>\n        protected void CheckSealed()\n        {\n            if ( _sealed )\n            {\n                throw new InvalidOperationException(SR.Format(SR.CannotChangeAfterSealed, \"SetterBase\"));\n            }            \n        }\n\n        // Derived\n        private bool _sealed;\n    }\n        \n}\n\n","sourceCodeStart":32,"sourceCodeEnd":60,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/SetterBase.cs#L32-L60","documentation":"SetterBase.CheckSealed throws InvalidOperationException once the setter's owning style/template has been sealed (made immutable after first use). This is WPF's immutability guard: sealed style objects cannot be modified, so any mutation after sealing fails with 'CannotChangeAfterSealed'.","triggerScenarios":"Modifying a Setter (Property or Value) after its style has been applied to an element or the style was sealed via style.Seal(); e.g. `setter.Value = x` on a setter retrieved from an applied style.","commonSituations":"Caching Style instances and mutating them later; tweaking a shared theme style at runtime; iterating Styles from FindResource and editing them.","solutions":["Create a new Style/Setter instance (possibly BasedOn the original) instead of mutating the sealed one.","If a style must be modified before use, do all mutations before applying it to any element.","For shared mutable styles, keep an unsealed master copy and generate fresh styles per modification."],"exampleFix":"// before\nvar style = (Style)button.FindResource(typeof(Button));\nstyle.Setters[0].Value = Brushes.Green; // sealed\n// after\nvar newStyle = new Style(typeof(Button), style);\nnewStyle.Setters.Add(new Setter(Control.BackgroundProperty, Brushes.Green));\nbutton.Style = newStyle;","handlingStrategy":"try-catch","validationCode":"if (setter.IsSealed) throw new InvalidOperationException(\"Create a new Setter instead of mutating\");","typeGuard":null,"tryCatchPattern":"try { setter.Value = newValue; }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"CannotChangeAfterSealed\")) { /* rebuild style */ }","preventionTips":["Treat styles and setters retrieved from applied elements as read-only","Create derived styles with BasedOn instead of editing sealed ones"],"tags":["wpf","style","sealed","immutability"],"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"}