{"record":{"id":"d4afe32135ac9c73","repo":"dotnet/wpf","slug":"sr-collectiononlyacceptscommandbindings","errorCode":null,"errorMessage":"SR.CollectionOnlyAcceptsCommandBindings","messagePattern":"SR\\.CollectionOnlyAcceptsCommandBindings","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/CommandBindingCollection.cs","lineNumber":130,"sourceCode":"        /// <param name=\"commandBinding\">CommandBinding object to remove</param>\n        void IList.Remove(object commandBinding)\n        {\n            Remove(commandBinding as CommandBinding);\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                if (value is not CommandBinding commandBinding)\n                    throw new NotSupportedException(SR.CollectionOnlyAcceptsCommandBindings);\n\n                this[index] = commandBinding;\n            }\n        }\n#endregion Implementation of IList \n        /// <summary>\n        /// Indexing operator\n        /// </summary>\n        public CommandBinding  this[int index]\n        {\n            get\n            {\n                return (_innerCBList?[index]);\n            }\n            set\n            {\n                _innerCBList?[index] = value;\n            }","sourceCodeStart":112,"sourceCodeEnd":148,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/CommandBindingCollection.cs#L112-L148","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Only assign CommandBinding instances through the indexer.","If value may be null or another type, filter it first or use Add(new CommandBinding(...)).","Build a new collection from validated items instead of mutating index-by-index."],"exampleFix":"// before\n((IList)element.CommandBindings)[0] = someObject;\n// after\nif (someObject is CommandBinding cb)\n    ((IList)element.CommandBindings)[0] = cb;","handlingStrategy":"type-guard","validationCode":"if (value is CommandBinding) { ((IList)element.CommandBindings)[index] = value; }","typeGuard":"static bool IsCommandBinding(object value) => value is CommandBinding;","tryCatchPattern":"try { ((IList)element.CommandBindings)[index] = value; }\ncatch (NotSupportedException) { /* value was not a CommandBinding */ }","preventionTips":["Work with the strongly-typed collection API instead of raw IList.","Keep CommandBindings in List<CommandBinding> internally and rebuild the collection on change.","Never store null into indexer slots of WPF collections."],"tags":["wpf","commandbinding","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-21T21:30:21.729Z"}