{"record":{"id":"449010c3cea47588","repo":"HandyOrg/HandyControl","slug":"exceptionstringtable-449010","errorCode":null,"errorMessage":"ExceptionStringTable.CannotHostTriggerCollectionMultipleTimesExceptionMessage","messagePattern":"ExceptionStringTable\\.CannotHostTriggerCollectionMultipleTimesExceptionMessage","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Shared/System.Windows.Interactivity/Interaction.cs","lineNumber":67,"sourceCode":"            return;\n        if (newValue.AssociatedObject != null)\n            throw new InvalidOperationException(ExceptionStringTable\n                .CannotHostBehaviorCollectionMultipleTimesExceptionMessage);\n        newValue.Attach(obj);\n    }\n\n    private static void OnTriggersChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)\n    {\n        var oldValue = args.OldValue as TriggerCollection;\n        var newValue = args.NewValue as TriggerCollection;\n        if (Equals(oldValue, newValue))\n            return;\n        if (oldValue?.AssociatedObject != null)\n            oldValue.Detach();\n        if (newValue == null || obj == null)\n            return;\n        if (newValue.AssociatedObject != null)\n            throw new InvalidOperationException(ExceptionStringTable\n                .CannotHostTriggerCollectionMultipleTimesExceptionMessage);\n        newValue.Attach(obj);\n    }\n\n    internal static bool IsElementLoaded(FrameworkElement element)\n    {\n        return element.IsLoaded;\n    }\n}\n","sourceCodeStart":49,"sourceCodeEnd":77,"githubUrl":"https://github.com/HandyOrg/HandyControl/blob/2c0875ebd67326e0c67282967e3e809c69282fee/src/Shared/System.Windows.Interactivity/Interaction.cs#L49-L77","documentation":"Analogous to behaviors: a TriggerCollection already attached to an object (AssociatedObject != null) cannot be attached to another. Interaction.OnTriggersChanged throws InvalidOperationException CannotHostTriggerCollectionMultipleTimesExceptionMessage when the newly assigned trigger collection is already hosted.","triggerScenarios":"Assigning a TriggerCollection instance whose AssociatedObject is set to a second element via Interaction.SetTriggers / the TriggersProperty change callback.","commonSituations":"Sharing one TriggerCollection across multiple controls in code; a shared XAML resource (collection) referenced by several elements; reparenting elements that carry the same collection instance.","solutions":["Create a fresh TriggerCollection per host element.","Detach/remove the collection from the old host first so its AssociatedObject resets.","Use x:Shared=\"False\" on shared XAML collection resources.","Set collections individually instead of copying dependency property values between elements."],"exampleFix":"// before\nvar triggers = new TriggerCollection { new EventTrigger { EventName = \"Click\" } };\nInteraction.SetTriggers(button1, triggers);\nInteraction.SetTriggers(button2, triggers); // throws\n// after\nInteraction.SetTriggers(button1, new TriggerCollection { new EventTrigger { EventName = \"Click\" } });\nInteraction.SetTriggers(button2, new TriggerCollection { new EventTrigger { EventName = \"Click\" } });","handlingStrategy":"validation","validationCode":"if (sharedTriggers.AssociatedObject != null)\n    sharedTriggers = new TriggerCollection(sharedTriggers); // clone before re-hosting","typeGuard":"static bool CanHostTriggers(TriggerCollection c) => c.AssociatedObject == null;","tryCatchPattern":"try { Interaction.SetTriggers(element, triggers); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"TriggerCollectionMultipleTimes\")) {\n    // assign a fresh TriggerCollection instance\n}","preventionTips":["One TriggerCollection instance per host element.","Use x:Shared=\"False\" on shared trigger collection resources.","Avoid copying Triggers dependency property values between elements.","Build trigger collections per element in code."],"tags":["wpf","interactivity","trigger-collection","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"}