dotnet/wpf · error · ArgumentException

SR.CollectionContainerMustBeUniqueForComposite

Error message

SR.CollectionContainerMustBeUniqueForComposite

What it means

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.

Solutions

  1. Create a new CollectionContainer instance for each entry instead of reusing an existing one
  2. Check CompositeCollection.Contains(cc) before calling Add/Insert
  3. Remove the existing container first (Remove/cc removal) before re-adding it
  4. If the same source collection is needed twice, wrap it in two separate CollectionContainer objects only if duplication is truly intended

Example fix

// before
composite.Add(existingContainer); // throws if already present
// after
if (!composite.Contains(existingContainer))
    composite.Add(existingContainer);
Defensive patterns

Strategy: validation

Validate before calling

if (cc == null) throw new ArgumentNullException(nameof(cc));
if (composite.Contains(cc)) return; // or throw with clear message
composite.Add(cc);

Try / catch

try { composite.Add(cc); }
catch (ArgumentException ex) when (ex.ParamName == "cc")
{
    // container already present; skip or log
}

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/c4e0177d876d312a. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/CompositeCollection.cs:475

        private void Initialize(ArrayList internalList)
        {
            _internalList = internalList;
        }

        // ArrayList that holds collection containers as well as single items
        private ArrayList InternalList
        {
            get
            {
                return _internalList;
            }
        }

        // Hook up to a newly-added CollectionContainer
        private void AddCollectionContainer(CollectionContainer cc)
        {
            if (InternalList.Contains(cc))
                throw new ArgumentException(SR.CollectionContainerMustBeUniqueForComposite, nameof(cc));

            CollectionChangedEventManager.AddHandler(cc, OnContainedCollectionChanged);

#if DEBUG
            _hasRepeatedCollectionIsValid = false;
#endif
        }

        // Unhook a newly-deleted CollectionContainer
        private void RemoveCollectionContainer(CollectionContainer cc)
        {
            CollectionChangedEventManager.RemoveHandler(cc, OnContainedCollectionChanged);

#if DEBUG
            _hasRepeatedCollectionIsValid = false;
#endif
        }

View on GitHub (pinned to 81131a70a4)