{"record":{"id":"bbe253874119ee0a","repo":"dotnet/wpf","slug":"sr-format-sr-cannotchangeaftersealed-style","errorCode":null,"errorMessage":"SR.Format(SR.CannotChangeAfterSealed, \"Style\")","messagePattern":"SR\\.Format\\(SR\\.CannotChangeAfterSealed, \"Style\"\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Style.cs","lineNumber":158,"sourceCode":"        [Localizability(LocalizationCategory.NeverLocalize)]\n        public Type TargetType\n        {\n            get\n            {\n                // Verify Context Access\n                VerifyAccess();\n\n                return _targetType;\n            }\n\n            set\n            {\n                // Verify Context Access\n                VerifyAccess();\n\n                if (_sealed)\n                {\n                    throw new InvalidOperationException(SR.Format(SR.CannotChangeAfterSealed, \"Style\"));\n                }\n\n                ArgumentNullException.ThrowIfNull(value);\n\n                if (!typeof(FrameworkElement).IsAssignableFrom(value) &&\n                    !typeof(FrameworkContentElement).IsAssignableFrom(value) &&\n                    !(DefaultTargetType == value))\n                {\n                    throw new ArgumentException(SR.Format(SR.MustBeFrameworkDerived, value.Name));\n                }\n\n                _targetType = value;\n\n                SetModified(TargetTypeID);\n            }\n        }\n\n        /// <summary>","sourceCodeStart":140,"sourceCodeEnd":176,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Style.cs#L140-L176","documentation":"Style.TargetType setter throws InvalidOperationException (SR.CannotChangeAfterSealed \"Style\") when the style has already been sealed. A Style is sealed once it has been applied to elements or explicitly Seal()ed, after which its properties are immutable.","triggerScenarios":"Assigning Style.TargetType after calling style.Seal() or after the style has been applied to a FrameworkElement/FrameworkContentElement (e.g. via Style property, implicit style, or BasedOn usage that forced sealing).","commonSituations":"Mutating a shared style retrieved from Resources that is already in use by rendered controls; changing TargetType in a window/page after InitializeComponent applied the resources; sharing one Style instance across windows and tweaking it later.","solutions":["Create a new Style instance with the desired TargetType and assign that instead.","If the style is in XAML resources, define a new keyed resource rather than mutating the applied one.","If mutation is required, do it before the style is applied/sealed (before InitializeComponent completes or before the first assignment to a control's Style).","Call CheckSeal-free approach: derive a new style with BasedOn and the new TargetType."],"exampleFix":"// before\nstyle.Seal();\nstyle.TargetType = typeof(Button); // throws\n// after\nvar newStyle = new Style(typeof(Button)) { BasedOn = style };\nbutton.Style = newStyle;","handlingStrategy":"validation","validationCode":"// Style has no public IsSealed check before WPF 4.x era APIs; guard by applying changes before first use\nif (!styleApplied) style.TargetType = typeof(Button);","typeGuard":null,"tryCatchPattern":"try { style.TargetType = t; }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"sealed\")) { style = new Style(t) { BasedOn = style }; }","preventionTips":["Treat styles as immutable once assigned to any element or placed in a used resource dictionary.","Configure all style properties before applying the style.","Never share a single mutable Style instance across windows/components that need different TargetTypes; create derived styles with BasedOn."],"tags":["invalid-state","style","wpf","sealed"],"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"}