{"record":{"id":"eac703c034393c72","repo":"dotnet/wpf","slug":"sr-collectiononlyacceptsinputbindings","errorCode":null,"errorMessage":"SR.CollectionOnlyAcceptsInputBindings","messagePattern":"SR\\.CollectionOnlyAcceptsInputBindings","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/InputBindingCollection.cs","lineNumber":144,"sourceCode":"        void IList.Remove(object inputBinding)\n        {\n            this.Remove(inputBinding as InputBinding);\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                InputBinding inputBinding = value as InputBinding;\n                if (inputBinding == null)\n                   throw new NotSupportedException(SR.CollectionOnlyAcceptsInputBindings);\n\n                this[index] = inputBinding;\n            }\n        }\n\n#endregion Implementation of IList\n        /// <summary>\n        /// Indexing operator\n        /// </summary>\n        public InputBinding this[int index]\n        {\n            get\n            {\n                if (_innerBindingList != null)\n                {\n                    return _innerBindingList[index];\n                }\n                else","sourceCodeStart":126,"sourceCodeEnd":162,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/InputBindingCollection.cs#L126-L162","documentation":"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.","triggerScenarios":"Setting collection[index] = value where 'value as InputBinding' yields null — null or any non-InputBinding object — via the indexer/IList set accessor.","commonSituations":"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.","solutions":["Assign only InputBinding-derived instances (KeyBinding, MouseBinding).","Guard with 'if (value is InputBinding ib) collection[index] = ib;'.","Rebuild the collection from validated InputBinding items."],"exampleFix":"// before\n((IList)element.InputBindings)[0] = gesture;\n// after\nif (gesture is InputBinding ib)\n    ((IList)element.InputBindings)[0] = ib;","handlingStrategy":"type-guard","validationCode":"if (value is InputBinding) { ((IList)element.InputBindings)[index] = value; }","typeGuard":"static bool IsInputBinding(object value) => value is InputBinding;","tryCatchPattern":"try { ((IList)element.InputBindings)[index] = value; }\ncatch (NotSupportedException) { /* value is null or not an InputBinding */ }","preventionTips":["Only store InputBinding-derived types (KeyBinding, MouseBinding) in InputBindings.","Avoid mixing InputGesture objects into InputBinding collections.","Use the typed collection members rather than casting to IList."],"tags":["wpf","inputbinding","collection"],"backgroundTag":"type-mismatch","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}