{"record":{"id":"c4e0177d876d312a","repo":"dotnet/wpf","slug":"sr-collectioncontainermustbeuniqueforcomposite","errorCode":null,"errorMessage":"SR.CollectionContainerMustBeUniqueForComposite","messagePattern":"SR\\.CollectionContainerMustBeUniqueForComposite","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/CompositeCollection.cs","lineNumber":475,"sourceCode":"        private void Initialize(ArrayList internalList)\n        {\n            _internalList = internalList;\n        }\n\n        // ArrayList that holds collection containers as well as single items\n        private ArrayList InternalList\n        {\n            get\n            {\n                return _internalList;\n            }\n        }\n\n        // Hook up to a newly-added CollectionContainer\n        private void AddCollectionContainer(CollectionContainer cc)\n        {\n            if (InternalList.Contains(cc))\n                throw new ArgumentException(SR.CollectionContainerMustBeUniqueForComposite, nameof(cc));\n\n            CollectionChangedEventManager.AddHandler(cc, OnContainedCollectionChanged);\n\n#if DEBUG\n            _hasRepeatedCollectionIsValid = false;\n#endif\n        }\n\n        // Unhook a newly-deleted CollectionContainer\n        private void RemoveCollectionContainer(CollectionContainer cc)\n        {\n            CollectionChangedEventManager.RemoveHandler(cc, OnContainedCollectionChanged);\n\n#if DEBUG\n            _hasRepeatedCollectionIsValid = false;\n#endif\n        }\n","sourceCodeStart":457,"sourceCodeEnd":493,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/CompositeCollection.cs#L457-L493","documentation":"CompositeCollection can contain each CollectionContainer only once. When a CollectionContainer instance already present in the composite is added again (via Add or Insert), CompositeCollection.AddCollectionContainer throws ArgumentException because duplicate containers would hook the same underlying collection's change events multiple times and appear more than once in the view.","triggerScenarios":"Calling CompositeCollection.Add(cc) or Insert(index, cc) with a CollectionContainer that is already in the CompositeCollection's InternalList (e.g. re-adding the same container instance after a refresh, or sharing one CollectionContainer object across multiple Add calls).","commonSituations":"Building a composite of several collections in XAML/code where the same CollectionContainer resource is referenced twice; programmatically rebuilding a CompositeCollection by re-adding existing containers instead of creating new ones.","solutions":["Create a new CollectionContainer instance for each entry instead of reusing an existing one","Check CompositeCollection.Contains(cc) before calling Add/Insert","Remove the existing container first (Remove/cc removal) before re-adding it","If the same source collection is needed twice, wrap it in two separate CollectionContainer objects only if duplication is truly intended"],"exampleFix":"// before\ncomposite.Add(existingContainer); // throws if already present\n// after\nif (!composite.Contains(existingContainer))\n    composite.Add(existingContainer);","handlingStrategy":"validation","validationCode":"if (cc == null) throw new ArgumentNullException(nameof(cc));\nif (composite.Contains(cc)) return; // or throw with clear message\ncomposite.Add(cc);","typeGuard":null,"tryCatchPattern":"try { composite.Add(cc); }\ncatch (ArgumentException ex) when (ex.ParamName == \"cc\")\n{\n    // container already present; skip or log\n}","preventionTips":["Track which CollectionContainer instances are already registered in the CompositeCollection","Never reuse CollectionContainer instances across Add calls; create a fresh one per entry","Wrap composite construction in a helper that de-duplicates containers"],"tags":["wpf","data-binding","argumentexception","compositecollection"],"backgroundTag":"invalid-argument-value","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}