{"record":{"id":"e4a828c99bd19bc4","repo":"dotnet/wpf","slug":"sr-collectiononlyacceptsinputgestures","errorCode":null,"errorMessage":"SR.CollectionOnlyAcceptsInputGestures","messagePattern":"SR\\.CollectionOnlyAcceptsInputGestures","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/InputGestureCollection.cs","lineNumber":141,"sourceCode":"                throw new NotSupportedException(SR.ReadOnlyInputGesturesCollection);\n            \n            this.Remove(inputGesture as InputGesture);\n        }\n\n        /// <summary>\n        /// Indexing operator\n        /// </summary>\n        object IList.this[int index]\n        {\n            get\n            { \n                return this[index]; \n            }\n            set \n            {\n                InputGesture inputGesture = value as InputGesture;\n                if (inputGesture == null)\n                    throw new NotSupportedException(SR.CollectionOnlyAcceptsInputGestures);\n\n                this[index] = inputGesture;\n            }\n        }\n#endregion Implementation of IList \n\n#region Implementation of Enumerable\n         /// <summary>\n         /// IEnumerable.GetEnumerator - For Enumeration purposes\n         /// </summary>\n         /// <returns></returns>\n         public IEnumerator GetEnumerator()\n         {\n             if (_innerGestureList != null)\n                 return _innerGestureList.GetEnumerator();\n\n             List<InputGesture> list = new List<InputGesture>(0);\n             return list.GetEnumerator();","sourceCodeStart":123,"sourceCodeEnd":159,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/InputGestureCollection.cs#L123-L159","documentation":"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.","triggerScenarios":"Assigning via the non-generic IList indexer ((IList)gestures)[i] = obj where obj is not an InputBinding/InputGesture, so the 'as' cast yields null.","commonSituations":"Reflection or generic collection-copy code (e.g. copying items from ArrayList or data-bound collections) that writes arbitrary objects into the list.","solutions":["Only assign InputGesture instances (KeyGesture, MouseGesture)","Use the typed indexer/public API instead of IList","Filter/convert items before copying into the collection"],"exampleFix":"// before\n((IList)gestures)[0] = someObject; // throws\n// after\nif (someObject is InputGesture g) { gestures[0] = g; }","handlingStrategy":"type-guard","validationCode":"if (value is InputGesture) gestures[index] = (InputGesture)value;","typeGuard":"bool IsInputGesture(object o) => o is InputGesture;","tryCatchPattern":"try { ((IList)gestures)[i] = value; }\ncatch (NotSupportedException) { /* value was not an InputGesture */ }","preventionTips":["Only put InputGesture instances in these collections","Avoid non-generic IList indexer writes","Sanitize data from config before inserting"],"tags":["wpf","inputgesture","collection","type-mismatch"],"backgroundTag":"type-mismatch","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"}