{"record":{"id":"af269da2024bb837","repo":"dotnet/wpf","slug":"sr-triggeractionmustbelongtoasingletrigger","errorCode":null,"errorMessage":"SR.TriggerActionMustBelongToASingleTrigger","messagePattern":"SR\\.TriggerActionMustBelongToASingleTrigger","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/TriggerAction.cs","lineNumber":87,"sourceCode":"            get\n            {\n                return _containingTrigger;\n            }\n        }\n\n        /// <summary>\n        ///     Seal this TriggerAction to prevent further updates\n        /// </summary>\n        /// <remarks>\n        ///     TriggerActionCollection will call this method to seal individual\n        /// TriggerAction objects.  We do some check here then call the\n        /// parameter-less Seal() so subclasses can also do what they need to do.\n        /// </remarks>\n        internal void Seal( TriggerBase containingTrigger )\n        {\n            if( IsSealed && containingTrigger != _containingTrigger )\n            {\n                throw new InvalidOperationException(SR.TriggerActionMustBelongToASingleTrigger);\n            }\n            _containingTrigger = containingTrigger;\n            Seal();\n        }\n\n        /// <summary>\n        ///     A derived class overrideing Seal() should set object state such\n        /// that further changes are not allowed.  This is also a time to make\n        /// validation checks to see if all parameters make sense.\n        /// </summary>\n        internal override void Seal()\n        {\n            if( IsSealed )\n            {\n                throw new InvalidOperationException(SR.TriggerActionAlreadySealed);\n            }\n            base.Seal();\n        }","sourceCodeStart":69,"sourceCodeEnd":105,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/TriggerAction.cs#L69-L105","documentation":"TriggerAction.Seal(TriggerBase) enforces that a TriggerAction instance belongs to exactly one trigger. If the action is already sealed and a different containingTrigger tries to seal it, InvalidOperationException with SR.TriggerActionMustBelongToASingleTrigger is thrown, because sharing a sealed action across triggers would corrupt its owning-trigger state.","triggerScenarios":"Reusing one BeginStoryboard/TriggerAction instance in two different EventTriggers (or a trigger and another trigger) — the second Seal attempt with a different containing trigger throws.","commonSituations":"Caching TriggerAction objects (e.g. one BeginStoryboard) and adding the same instance to multiple EventTriggers across styles or controls.","solutions":["Create a separate TriggerAction instance for each trigger that needs one.","If sharing is intended, create a factory method returning a new action per trigger.","Check action.IsSealed before adding; if sealed and already owned by another trigger, construct a copy."],"exampleFix":"// before\nvar action = new BeginStoryboard(storyboard);\ntriggerA.Actions.Add(action);\ntriggerB.Actions.Add(action); // throws on seal of triggerB\n// after\ntriggerA.Actions.Add(new BeginStoryboard(storyboard));\ntriggerB.Actions.Add(new BeginStoryboard(storyboard));","handlingStrategy":"validation","validationCode":"if (action.IsSealed) throw new InvalidOperationException(\"Create a new TriggerAction instance for each trigger\");","typeGuard":"static bool CanAttach(TriggerAction a, TriggerBase t) => a != null && (!a.IsSealed || true /* cannot inspect owner; assume not shared */);","tryCatchPattern":"try { trigger.Actions.Add(action); }\ncatch (InvalidOperationException) { trigger.Actions.Add(CloneAction(action)); }","preventionTips":["Never add the same TriggerAction instance to two triggers","Use a factory method to create one action per trigger","Keep a 1:1 relationship between actions and their owning trigger"],"tags":["wpf","trigger-action","invalid-operation","object-reuse"],"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"}