{"record":{"id":"9d90b7b10dfcb386","repo":"dotnet/wpf","slug":"sr-format-sr-cannotchangeaftersealed-triggercollection","errorCode":null,"errorMessage":"SR.Format(SR.CannotChangeAfterSealed, \"TriggerCollection\")","messagePattern":"SR\\.Format\\(SR\\.CannotChangeAfterSealed, \"TriggerCollection\"\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/TriggerCollection.cs","lineNumber":131,"sourceCode":"        }\n        \n        // This may be null (i.e. when used in a style or template)\n        internal FrameworkElement Owner\n        {\n            get { return _owner; }\n        }\n\n\n        \n        #endregion InternalMethods\n\n        #region PrivateMethods\n\n        private void CheckSealed()\n        {\n            if (_sealed)\n            {\n                throw new InvalidOperationException(SR.Format(SR.CannotChangeAfterSealed, \"TriggerCollection\"));\n            }\n        }\n\n        private void TriggerBaseValidation(TriggerBase triggerBase)\n        {\n            ArgumentNullException.ThrowIfNull(triggerBase);\n        }\n\n        // Called by GenericCollection.tb when a trigger is added to the collection.\n        // We use this opportunity to hook it into the tree.\n        private void OnAdd( TriggerBase triggerBase )\n        {\n            // If we don't have an Owner (the Style/Template case), or the\n            // element isn't initialized yet, we don't need to do anything\n            if (Owner != null && Owner.IsInitialized)\n            {\n                EventTrigger.ProcessOneTrigger(Owner, triggerBase);\n            }","sourceCodeStart":113,"sourceCodeEnd":149,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/TriggerCollection.cs#L113-L149","documentation":"A TriggerCollection becomes sealed once the owning FrameworkElement/FrameworkContentElement (via its Style or template) has been initialized and the collection is no longer mutable. Any attempt to Clear, Insert, Remove, or Set items in a sealed collection throws this InvalidOperationException from CheckSealed. This protects trigger state that the framework depends on after styling has been applied.","triggerScenarios":"Calling triggers.Clear(), triggers.Add(...), triggers.Remove(...), or triggers[i] = ... on a TriggerCollection after it has been sealed, typically because the element's style/template context has been established and the collection instance is shared with the style.","commonSituations":"Mutating element.Triggers at runtime after the element is loaded/styled; sharing one TriggerCollection instance between multiple elements; modifying triggers inside a Loaded handler or after ApplyTemplate.","solutions":["Modify triggers before the element is styled/loaded, e.g. in XAML or before adding the element to the visual tree","Create a new collection/element if post-load trigger changes are required","Trigger style re-evaluation by rebuilding the Style and reassigning it rather than mutating a sealed collection","Check collection.IsSealed before mutating and branch to a rebuild path"],"exampleFix":"// before\nprotected override void OnLoaded(EventArgs e) {\n    Triggers.Clear(); // sealed -> throws\n}\n// after\nprotected override void OnInitialized(EventArgs e) {\n    Triggers.Add(new Trigger { Property = IsMouseOverProperty, Value = true });\n}","handlingStrategy":"validation","validationCode":"if (!triggers.IsSealed) { triggers.Add(newTrigger); } else { /* rebuild element or style */ }","typeGuard":"bool CanMutate(TriggerCollection c) => !c.IsSealed;","tryCatchPattern":"try { triggers.Add(t); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"TriggerCollection\")) { /* rebuild collection instead */ }","preventionTips":["Configure Triggers in XAML or before the element is loaded","Never share TriggerCollection instances between elements","Check IsSealed before any mutation"],"tags":["wpf","triggers","collection","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"}