{"record":{"id":"9c6dcf4ce9511490","repo":"dotnet/wpf","slug":"sr-format-sr-cannotchangeaftersealed-triggeractioncollection","errorCode":null,"errorMessage":"SR.Format(SR.CannotChangeAfterSealed, \"TriggerActionCollection\")","messagePattern":"SR\\.Format\\(SR\\.CannotChangeAfterSealed, \"TriggerActionCollection\"\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/TriggerActionCollection.cs","lineNumber":292,"sourceCode":"        internal DependencyObject Owner\n        {\n            get { return _owner; }\n            set\n            { \n                Debug.Assert (Owner == null);\n                _owner = value; \n            }\n        }\n\n        ///////////////////////////////////////////////////////////////////////\n        //  Private members\n\n        // Throw if a change is attempted at a time we're not allowing them\n        private void CheckSealed()\n        {\n            if ( _sealed )\n            {\n                throw new InvalidOperationException(SR.Format(SR.CannotChangeAfterSealed, \"TriggerActionCollection\"));\n            }            \n        }\n\n        // Throw if the given object isn't a TriggerAction\n        private TriggerAction VerifyIsTriggerAction(object value)\n        {\n            TriggerAction action = value as TriggerAction;\n\n            if ( action == null )\n            {\n                ArgumentNullException.ThrowIfNull(value);\n\n                throw new ArgumentException(SR.MustBeTriggerAction);\n            }\n\n            return action;\n        }\n","sourceCodeStart":274,"sourceCodeEnd":310,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/TriggerActionCollection.cs#L274-L310","documentation":"TriggerActionCollection.CheckSealed() throws InvalidOperationException (CannotChangeAfterSealed, 'TriggerActionCollection') from Clear, RemoveAt, Add, Insert, Remove and the constructor path when the collection is sealed. A trigger's action collection is sealed together with its owning trigger/style once applied, making it read-only.","triggerScenarios":"Calling Add/Insert/Remove/Clear/RemoveAt on a trigger's Actions (or Exits) collection after the parent Style/Template was sealed by application to an element.","commonSituations":"Dynamically adding actions to a trigger of an in-use style; a shared template whose triggers are mutated after being applied to multiple controls.","solutions":["Rebuild the Style/Trigger in a fresh instance and reassign it to the controls","Perform all collection edits before the style is applied (before control.Style is set)","Guard edits with a check of the trigger's IsSealed state"],"exampleFix":"// before\ntrigger.Actions.Add(newAction); // InvalidOperationException once style applied\n// after\nvar newTrigger = new Trigger { Property = trigger.Property, Value = trigger.Value };\nforeach (var a in trigger.Actions) newTrigger.Actions.Add(a);\nnewTrigger.Actions.Add(newAction);\nvar newStyle = new Style { TargetType = typeof(Button) };\nnewStyle.Triggers.Add(newTrigger);\nbutton.Style = newStyle;","handlingStrategy":"validation","validationCode":"if (!trigger.IsSealed) { trigger.Actions.Add(newAction); } else { /* rebuild trigger/style */ }","typeGuard":"static bool CanEditActions(TriggerBase t) => t != null && !t.IsSealed;","tryCatchPattern":"try { actions.Add(a); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"TriggerActionCollection\")) { /* rebuild style */ }","preventionTips":["Complete all Actions edits before applying the style","Prefer XAML-defined triggers over runtime edits","Treat sealed collections as read-only; create new ones instead"],"tags":["wpf","styles","sealed-object","collection-modified"],"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"}