dotnet/wpf · error · NotSupportedException

SR.CollectionOnlyAcceptsInputBindings

Error message

SR.CollectionOnlyAcceptsInputBindings

What it means

InputBindingCollection's IList indexer setter casts the value to InputBinding and throws NotSupportedException (SR.CollectionOnlyAcceptsInputBindings) when the value is null or not an InputBinding. The collection keeps only InputBinding items (e.g. KeyBinding, MouseBinding) in its internal list, tied to the owner element via inheritance context.

Solutions

  1. Assign only InputBinding-derived instances (KeyBinding, MouseBinding).
  2. Guard with 'if (value is InputBinding ib) collection[index] = ib;'.
  3. Rebuild the collection from validated InputBinding items.

Example fix

// before
((IList)element.InputBindings)[0] = gesture;
// after
if (gesture is InputBinding ib)
    ((IList)element.InputBindings)[0] = ib;
Defensive patterns

Strategy: type-guard

Validate before calling

if (value is InputBinding) { ((IList)element.InputBindings)[index] = value; }

Type guard

static bool IsInputBinding(object value) => value is InputBinding;

Try / catch

try { ((IList)element.InputBindings)[index] = value; }
catch (NotSupportedException) { /* value is null or not an InputBinding */ }

Prevention

When it happens

Trigger: Setting collection[index] = value where 'value as InputBinding' yields null — null or any non-InputBinding object — via the indexer/IList set accessor.

Common situations: Reflection or designer-generated code writing generic items into UIElement.InputBindings; copying items between CommandBinding and InputBinding collections; data-binding that supplies boxed non-collection types.

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/eac703c034393c72. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/InputBindingCollection.cs:144

        void IList.Remove(object inputBinding)
        {
            this.Remove(inputBinding as InputBinding);
        }

        /// <summary>
        /// Indexing operator
        /// </summary>
        object IList.this[int index]
        {
            get
            {
                return this[index];
            }
            set
            {
                InputBinding inputBinding = value as InputBinding;
                if (inputBinding == null)
                   throw new NotSupportedException(SR.CollectionOnlyAcceptsInputBindings);

                this[index] = inputBinding;
            }
        }

#endregion Implementation of IList
        /// <summary>
        /// Indexing operator
        /// </summary>
        public InputBinding this[int index]
        {
            get
            {
                if (_innerBindingList != null)
                {
                    return _innerBindingList[index];
                }
                else

View on GitHub (pinned to 81131a70a4)