{"record":{"id":"7141d8b4b850f7a7","repo":"HandyOrg/HandyControl","slug":"exceptionstringtable-interaction","errorCode":null,"errorMessage":"ExceptionStringTable.CannotHostBehaviorCollectionMultipleTimesExceptionMessage","messagePattern":"ExceptionStringTable\\.CannotHostBehaviorCollectionMultipleTimesExceptionMessage","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Shared/System.Windows.Interactivity/Interaction.cs","lineNumber":51,"sourceCode":"        {\n            behaviorCollection = new BehaviorCollection();\n            obj.SetValue(BehaviorsProperty, behaviorCollection);\n        }\n        return behaviorCollection;\n    }\n\n    private static void OnBehaviorsChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)\n    {\n        var oldValue = (BehaviorCollection) args.OldValue;\n        var newValue = (BehaviorCollection) args.NewValue;\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                .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);","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/HandyOrg/HandyControl/blob/2c0875ebd67326e0c67282967e3e809c69282fee/src/Shared/System.Windows.Interactivity/Interaction.cs#L33-L69","documentation":"A BehaviorCollection is already attached to some object if its AssociatedObject is non-null. Interaction.OnBehaviorsChanged throws InvalidOperationException CannotHostBehaviorCollectionMultipleTimesExceptionMessage when the new collection assigned to a second element is already hosted elsewhere, preserving the one-collection-per-host invariant.","triggerScenarios":"Assigning the same BehaviorCollection instance to Interaction.GetBehaviors(obj2) (via the BehaviorsProperty change handler) when that collection's AssociatedObject is already set to another element.","commonSituations":"Sharing a BehaviorCollection instance between two controls in code; moving a control in the visual tree so its Behaviors property value gets reused; copying dependency property values that carry the same collection instance; resource reuse in styles where the collection is a shared resource.","solutions":["Give each host its own new BehaviorCollection instance.","Remove the collection from the previous host so its AssociatedObject is cleared before reassigning.","Use x:Shared=\"False\" for collection resources in XAML so each reference gets a fresh instance.","Define collections per-control in a Style Setter rather than sharing instances."],"exampleFix":"// before\nvar collection = new BehaviorCollection { new MyBehavior() };\nelement1.SetValue(Interaction.BehaviorsProperty, collection);\nelement2.SetValue(Interaction.BehaviorsProperty, collection); // throws\n// after\nelement1.SetValue(Interaction.BehaviorsProperty, new BehaviorCollection { new MyBehavior() });\nelement2.SetValue(Interaction.BehaviorsProperty, new BehaviorCollection { new MyBehavior() });","handlingStrategy":"validation","validationCode":"if (sharedCollection.AssociatedObject != null)\n    sharedCollection = new BehaviorCollection(sharedCollection); // clone before re-hosting","typeGuard":"static bool CanHostCollection(BehaviorCollection c) => c.AssociatedObject == null;","tryCatchPattern":"try { Interaction.SetBehaviors(element, collection); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"BehaviorCollectionMultipleTimes\")) {\n    // assign a fresh BehaviorCollection instance\n}","preventionTips":["One BehaviorCollection instance per host element.","Use x:Shared=\"False\" for XAML collection resources.","Clone collections when copying property values between elements.","Never pull the same collection out of a shared resource for multiple controls."],"tags":["wpf","interactivity","behavior-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"}