{"record":{"id":"b68a38567ebdfe6d","repo":"HandyOrg/HandyControl","slug":"exceptionstringtable-triggeractioncollection","errorCode":null,"errorMessage":"ExceptionStringTable.CannotHostTriggerActionMultipleTimesExceptionMessage","messagePattern":"ExceptionStringTable\\.CannotHostTriggerActionMultipleTimesExceptionMessage","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Shared/System.Windows.Interactivity/TriggerActionCollection.cs","lineNumber":20,"sourceCode":"using System.Windows;\n\nnamespace HandyControl.Interactivity;\n\npublic class TriggerActionCollection : AttachableCollection<TriggerAction>\n{\n    internal TriggerActionCollection()\n    {\n    }\n\n    protected override Freezable CreateInstanceCore()\n    {\n        return new TriggerActionCollection();\n    }\n\n    internal override void ItemAdded(TriggerAction item)\n    {\n        if (item.IsHosted)\n            throw new InvalidOperationException(ExceptionStringTable\n                .CannotHostTriggerActionMultipleTimesExceptionMessage);\n        if (AssociatedObject != null)\n            item.Attach(AssociatedObject);\n        item.IsHosted = true;\n    }\n\n    internal override void ItemRemoved(TriggerAction item)\n    {\n        if (((IAttachedObject) item).AssociatedObject != null)\n            item.Detach();\n        item.IsHosted = false;\n    }\n\n    protected override void OnAttached()\n    {\n        foreach (var action in this)\n            action.Attach(AssociatedObject);\n    }","sourceCodeStart":2,"sourceCodeEnd":38,"githubUrl":"https://github.com/HandyOrg/HandyControl/blob/2c0875ebd67326e0c67282967e3e809c69282fee/src/Shared/System.Windows.Interactivity/TriggerActionCollection.cs#L2-L38","documentation":"TriggerActionCollection.ItemAdded throws InvalidOperationException when a TriggerAction that already has a host (IsHosted == true) is added to another collection. The Interactivity library forbids one TriggerAction instance from being attached to multiple hosts, since Attach hooks it to a single AssociatedObject. This guards the invariant that each action belongs to exactly one trigger/host.","triggerScenarios":"Adding the same TriggerAction instance (from XAML resources or code) into a second TriggerActionCollection, e.g. reusing a shared <TriggerAction> resource in multiple triggers or calling collection.Add(action) twice.","commonSituations":"Defining an action as a shared resource (x:Shared not considered) and referencing it from multiple triggers; programmatically moving an action between behaviors/triggers without removing it first; accidentally adding the same instance twice in code.","solutions":["Create a new TriggerAction instance for each trigger instead of sharing one resource instance","Remove the action from its current collection (or set IsHosted=false via a new instance) before adding elsewhere","In XAML, avoid using a single action defined in resources across multiple triggers; declare it inline per trigger"],"exampleFix":"// before\n<Trigger Action=\"{StaticResource SharedAction}\"/>\n<Trigger Action=\"{StaticResource SharedAction}\"/>\n// after\n<Trigger><local:MyAction/></Trigger>\n<Trigger><local:MyAction/></Trigger>","handlingStrategy":"validation","validationCode":"if (actions.Contains(action)) return; // or create a new instance\ncollection.Add(action);","typeGuard":null,"tryCatchPattern":"try { collection.Add(action); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"host\")) { collection.Add((TriggerAction)Activator.CreateInstance(action.GetType())); }","preventionTips":["One TriggerAction instance per trigger","Factory-create actions instead of sharing resources","Remove before re-adding"],"tags":["wpf","interactivity","invalid-state"],"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"}