dotnet/wpf · error · NotSupportedException

SR.CollectionOnlyAcceptsCommandBindings

Error message

SR.CollectionOnlyAcceptsCommandBindings

What it means

CommandBindingCollection's IList indexer setter only accepts CommandBinding instances. Assigning any other object via the indexer throws NotSupportedException with SR.CollectionOnlyAcceptsCommandBindings. The collection wraps an internal ArrayList (_innerCBList) that stores only CommandBinding items.

Solutions

  1. Only assign CommandBinding instances through the indexer.
  2. If value may be null or another type, filter it first or use Add(new CommandBinding(...)).
  3. Build a new collection from validated items instead of mutating index-by-index.

Example fix

// before
((IList)element.CommandBindings)[0] = someObject;
// after
if (someObject is CommandBinding cb)
    ((IList)element.CommandBindings)[0] = cb;
Defensive patterns

Strategy: type-guard

Validate before calling

if (value is CommandBinding) { ((IList)element.CommandBindings)[index] = value; }

Type guard

static bool IsCommandBinding(object value) => value is CommandBinding;

Try / catch

try { ((IList)element.CommandBindings)[index] = value; }
catch (NotSupportedException) { /* value was not a CommandBinding */ }

Prevention

When it happens

Trigger: Calling collection[index] = value where value is not a CommandBinding (e.g. null or an InputGesture/any object) through the explicit IList implementation or the indexer.

Common situations: Reflection-driven or data-binding code assigning generic items into the collection; porting code from loosely typed collections; casting an IList of UIElement.CommandBindings to object[] and writing back non-CommandBinding values.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/CommandBindingCollection.cs:130

        /// <param name="commandBinding">CommandBinding object to remove</param>
        void IList.Remove(object commandBinding)
        {
            Remove(commandBinding as CommandBinding);
        }

        /// <summary>
        /// Indexing operator
        /// </summary>
        object IList.this[int index]
        {
            get 
            { 
                return this[index]; 
            }
            set 
            {
                if (value is not CommandBinding commandBinding)
                    throw new NotSupportedException(SR.CollectionOnlyAcceptsCommandBindings);

                this[index] = commandBinding;
            }
        }
#endregion Implementation of IList 
        /// <summary>
        /// Indexing operator
        /// </summary>
        public CommandBinding  this[int index]
        {
            get
            {
                return (_innerCBList?[index]);
            }
            set
            {
                _innerCBList?[index] = value;
            }

View on GitHub (pinned to 81131a70a4)