{"record":{"id":"eebde4331b9802ae","repo":"HandyOrg/HandyControl","slug":"exceptionstringtable","errorCode":null,"errorMessage":"ExceptionStringTable.DuplicateItemInCollectionExceptionMessage","messagePattern":"ExceptionStringTable\\.DuplicateItemInCollectionExceptionMessage","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Shared/System.Windows.Interactivity/AttachableCollection`1.cs","lineNumber":69,"sourceCode":"        OnDetaching();\n        WritePreamble();\n        _associatedObject = null;\n        WritePostscript();\n    }\n\n    protected abstract void OnAttached();\n\n    protected abstract void OnDetaching();\n\n    internal abstract void ItemAdded(T item);\n\n    internal abstract void ItemRemoved(T item);\n\n    private void VerifyAdd(T item)\n    {\n        if (item == null) throw new ArgumentNullException(nameof(item));\n        if (_snapshot.Contains(item))\n            throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture,\n                ExceptionStringTable.DuplicateItemInCollectionExceptionMessage, new object[]\n                {\n                    typeof(T).Name,\n                    GetType().Name\n                }));\n    }\n\n    private void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)\n    {\n        switch (e.Action)\n        {\n            case NotifyCollectionChangedAction.Add:\n                var enumerator1 = e.NewItems.GetEnumerator();\n                try\n                {\n                    while (enumerator1.MoveNext())\n                    {\n                        var current = (T) enumerator1.Current;","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/HandyOrg/HandyControl/blob/2c0875ebd67326e0c67282967e3e809c69282fee/src/Shared/System.Windows.Interactivity/AttachableCollection`1.cs#L51-L87","documentation":"AttachableCollection<T>.VerifyAdd guards that each item is added at most once and is non-null. On CollectionChanged it verifies additions; if the item already exists in the internal _snapshot it throws InvalidOperationException with a localized message from ExceptionStringTable naming the duplicate type and collection type.","triggerScenarios":"Adding the same behavior/trigger instance (e.g. a Behavior<T>) to a collection — or to two attachable collections — where it is already present; also triggered indirectly by re-parenting an element that carries an already-attached behavior.","commonSituations":"Reusing a single Behavior instance across multiple elements or multiple Interaction.GetBehaviors() collections; XAML resource shared via x:Shared=\"false\" duplicates or style setters re-adding the same instance; adding during collection-change reentrancy.","solutions":["Check Contains before adding: if (!coll.Contains(item)) coll.Add(item)","Create a new behavior instance per target element instead of sharing one instance","Use TryGetValue/deduplication logic when wiring behaviors programmatically"],"exampleFix":"// before\nvar b = new MyBehavior();\nInteraction.GetBehaviors(el1).Add(b);\nInteraction.GetBehaviors(el2).Add(b); // duplicate — throws\n// after\nInteraction.GetBehaviors(el1).Add(new MyBehavior());\nInteraction.GetBehaviors(el2).Add(new MyBehavior());","handlingStrategy":"validation","validationCode":"if (item == null) throw new ArgumentNullException(nameof(item));\nif (!behaviors.Contains(item)) behaviors.Add(item);","typeGuard":"bool CanAdd<T>(ICollection<T> coll, T item) => item != null && !coll.Contains(item);","tryCatchPattern":"try { behaviors.Add(behavior); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"DuplicateItemInCollectionExceptionMessage\") || ex.Message.Contains(\"already\")) { /* item already attached — reuse or skip */ }","preventionTips":["Never share a single Behavior/Trigger instance across multiple elements","Create a fresh instance per attach target","Deduplicate programmatic adds with a Contains check","Be careful with x:Shared=\"false\" resources that can yield repeated adds"],"tags":["collection","duplicate-item","wpf","interactivity"],"backgroundTag":"duplicate-item","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"}