{"record":{"id":"bdd6d0090ebf3cf6","repo":"HandyOrg/HandyControl","slug":"exceptionstringtable-triggeraction","errorCode":null,"errorMessage":"ExceptionStringTable.CannotHostTriggerActionMultipleTimesExceptionMessage","messagePattern":"ExceptionStringTable\\.CannotHostTriggerActionMultipleTimesExceptionMessage","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Shared/System.Windows.Interactivity/TriggerAction.cs","lineNumber":71,"sourceCode":"        get\n        {\n            ReadPreamble();\n            return _isHosted;\n        }\n        set\n        {\n            WritePreamble();\n            _isHosted = value;\n            WritePostscript();\n        }\n    }\n\n    public void Attach(DependencyObject dependencyObject)\n    {\n        if (!Equals(dependencyObject, AssociatedObject))\n        {\n            if (AssociatedObject != null)\n                throw new InvalidOperationException(ExceptionStringTable\n                    .CannotHostTriggerActionMultipleTimesExceptionMessage);\n            if (dependencyObject != null &&\n                !AssociatedObjectTypeConstraint.IsInstanceOfType(dependencyObject))\n                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture,\n                    ExceptionStringTable.TypeConstraintViolatedExceptionMessage,\n                    new object[]\n                        {GetType().Name, dependencyObject.GetType().Name, AssociatedObjectTypeConstraint.Name}));\n            WritePreamble();\n            _associatedObject = dependencyObject;\n            WritePostscript();\n            OnAttached();\n        }\n    }\n\n    public void Detach()\n    {\n        OnDetaching();\n        WritePreamble();","sourceCodeStart":53,"sourceCodeEnd":89,"githubUrl":"https://github.com/HandyOrg/HandyControl/blob/2c0875ebd67326e0c67282967e3e809c69282fee/src/Shared/System.Windows.Interactivity/TriggerAction.cs#L53-L89","documentation":"A TriggerAction can be hosted by only one trigger/object at a time. TriggerAction.Attach throws InvalidOperationException CannotHostTriggerActionMultipleTimesExceptionMessage when called while AssociatedObject is already set to a different object, enforced when a trigger collection/process adds the action (ItemAdded).","triggerScenarios":"Calling action.Attach(newObject) or adding the same TriggerAction instance (directly or into an EventTrigger.Actions collection) when its AssociatedObject is already non-null and different.","commonSituations":"Reusing one InvokeCommandAction/CallMethodAction across multiple triggers or controls; adding the same action instance to two EventTriggers; re-attaching an action after reparenting its host without detaching.","solutions":["Create a new TriggerAction instance for each trigger/control.","Remove the action from its current trigger (and detach) before adding it elsewhere.","Check action.AssociatedObject == null before adding/attaching.","Build actions in code per-usage rather than sharing from a resource."],"exampleFix":"// before\nvar action = new InvokeCommandAction { CommandName = \"DoIt\" };\ntrigger1.Actions.Add(action);\ntrigger2.Actions.Add(action); // throws\n// after\ntrigger1.Actions.Add(new InvokeCommandAction { CommandName = \"DoIt\" });\ntrigger2.Actions.Add(new InvokeCommandAction { CommandName = \"DoIt\" });","handlingStrategy":"validation","validationCode":"if (action.AssociatedObject != null && !ReferenceEquals(action.AssociatedObject, target))\n    action = (TriggerAction)Activator.CreateInstance(action.GetType());","typeGuard":"static bool CanAttachAction(TriggerAction a, DependencyObject target) =>\n    a.AssociatedObject == null && target != null && a.AssociatedObjectTypeConstraint.IsInstanceOfType(target);","tryCatchPattern":"try { trigger.Actions.Add(action); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"CannotHostTriggerActionMultipleTimes\")) {\n    // create a new action instance for this trigger\n}","preventionTips":["One TriggerAction instance per trigger/host.","Remove actions from old triggers before reusing elsewhere.","Check AssociatedObject == null before adding.","Don't reuse action instances across data templates."],"tags":["wpf","interactivity","trigger-action","invalid-state-transition","shared-instance"],"backgroundTag":"invalid-state-transition","analyzedSha":"2c0875ebd67326e0c67282967e3e809c69282fee","analyzedAt":"2026-09-14T14:45:29.754Z","contentChangedAt":"2026-09-14T14:45:29.754Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}