{"record":{"id":"4d74d9bdefe090d5","repo":"dotnet/wpf","slug":"sr-format-sr-cannotchangeaftersealed-eventtrigger","errorCode":null,"errorMessage":"SR.Format(SR.CannotChangeAfterSealed, \"EventTrigger\")","messagePattern":"SR\\.Format\\(SR\\.CannotChangeAfterSealed, \"EventTrigger\"\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/EventTrigger.cs","lineNumber":96,"sourceCode":"        }\n        \n        /// <summary>\n        ///     The Event that will activate this trigger - one must be specified\n        /// before an event trigger is meaningful.\n        /// </summary>\n        public RoutedEvent RoutedEvent\n        {\n            get\n            {\n                return _routedEvent;\n            }\n            set\n            {\n                ArgumentNullException.ThrowIfNull(value);\n\n                if ( IsSealed )\n                {\n                    throw new InvalidOperationException(SR.Format(SR.CannotChangeAfterSealed, \"EventTrigger\"));\n                }\n\n                // When used as an element trigger, we don't actually need to seal\n                //  to ensure cross-thread usability.  However, if we *are* fixed on\n                //  listening to an event already, don't allow this change.\n                if( _routedEventHandler != null )\n                {\n                    // Recycle the Seal error message.\n                    throw new InvalidOperationException(SR.Format(SR.CannotChangeAfterSealed, \"EventTrigger\"));\n                }\n\n                _routedEvent = value;\n            }\n        }\n\n        /// <summary>\n        ///     The x:Name of the object whose event shall trigger this \n        /// EventTrigger.   If null, then this is the object being Styled","sourceCodeStart":78,"sourceCodeEnd":114,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/EventTrigger.cs#L78-L114","documentation":"EventTrigger.RoutedEvent's setter throws InvalidOperationException (CannotChangeAfterSealed) if the trigger has been sealed. Sealing happens when the trigger becomes part of a Style/ControlTemplate in use, after which its configuration is immutable for thread-safety.","triggerScenarios":"Setting trigger.RoutedEvent after the EventTrigger was added to a Style.Triggers / FrameworkTemplate.Triggers collection that has been sealed.","commonSituations":"Mutating shared style triggers at runtime; trying to retarget an EventTrigger after the style was applied to controls; editing template triggers post-layout.","solutions":["Set RoutedEvent before adding the trigger to the style/template","Create a new EventTrigger with the desired RoutedEvent and replace it in the collection (before the style is applied)","Recreate the entire Style/Template rather than mutating sealed triggers"],"exampleFix":"// before\nvar trig = new EventTrigger();\nstyle.Triggers.Add(trig);\nApplyStyle();\ntrig.RoutedEvent = Button.ClickEvent; // throws\n// after\nvar trig = new EventTrigger { RoutedEvent = Button.ClickEvent };\nstyle.Triggers.Add(trig);","handlingStrategy":"validation","validationCode":"if (trigger.IsSealed) throw new InvalidOperationException(\"Create a new EventTrigger instead of mutating a sealed one\");","typeGuard":"bool CanConfigure(EventTrigger t) => !t.IsSealed;","tryCatchPattern":"try { trigger.RoutedEvent = ev; }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"sealed\")) { trigger = new EventTrigger { RoutedEvent = ev }; }","preventionTips":["Configure triggers fully before adding to styles/templates","Treat sealed style resources as immutable at runtime"],"tags":["wpf","eventtrigger","style","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-21T21:30:21.729Z"}