dotnet/wpf · error · NotSupportedException

SR.CollectionOnlyAcceptsInputGestures

Error message

SR.CollectionOnlyAcceptsInputGestures

What it means

NotSupportedException thrown by IList.set on InputGestureCollection when a value that is not an InputGesture is assigned through the non-generic IList interface; the collection guard rejects incompatible values inserted via the explicit interface implementation.

Solutions

  1. Only assign InputGesture instances (KeyGesture, MouseGesture)
  2. Use the typed indexer/public API instead of IList
  3. Filter/convert items before copying into the collection

Example fix

// before
((IList)gestures)[0] = someObject; // throws
// after
if (someObject is InputGesture g) { gestures[0] = g; }
Defensive patterns

Strategy: type-guard

Validate before calling

if (value is InputGesture) gestures[index] = (InputGesture)value;

Type guard

bool IsInputGesture(object o) => o is InputGesture;

Try / catch

try { ((IList)gestures)[i] = value; }
catch (NotSupportedException) { /* value was not an InputGesture */ }

Prevention

When it happens

Trigger: Assigning via the non-generic IList indexer ((IList)gestures)[i] = obj where obj is not an InputBinding/InputGesture, so the 'as' cast yields null.

Common situations: Reflection or generic collection-copy code (e.g. copying items from ArrayList or data-bound collections) that writes arbitrary objects into the list.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/InputGestureCollection.cs:141

                throw new NotSupportedException(SR.ReadOnlyInputGesturesCollection);
            
            this.Remove(inputGesture as InputGesture);
        }

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

                this[index] = inputGesture;
            }
        }
#endregion Implementation of IList 

#region Implementation of Enumerable
         /// <summary>
         /// IEnumerable.GetEnumerator - For Enumeration purposes
         /// </summary>
         /// <returns></returns>
         public IEnumerator GetEnumerator()
         {
             if (_innerGestureList != null)
                 return _innerGestureList.GetEnumerator();

             List<InputGesture> list = new List<InputGesture>(0);
             return list.GetEnumerator();

View on GitHub (pinned to 81131a70a4)