{"record":{"id":"117efe54347c47df","repo":"dotnet/wpf","slug":"sr-readonlyinputgesturescollection","errorCode":null,"errorMessage":"SR.ReadOnlyInputGesturesCollection","messagePattern":"SR\\.ReadOnlyInputGesturesCollection","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/InputGestureCollection.cs","lineNumber":99,"sourceCode":"        /// IndexOf\n        /// </summary>\n        /// <param name=\"value\"></param>\n        /// <returns></returns>\n        int IList.IndexOf(object value)\n        {\n            InputGesture inputGesture = value as InputGesture;\n            return ((inputGesture != null) ? this.IndexOf(inputGesture) : -1) ;\n        }\n\n        /// <summary>\n        ///  Insert\n        /// </summary>\n        /// <param name=\"index\">index at which to insert the item</param>\n        /// <param name=\"value\">item value to insert</param>\n        void IList.Insert(int index, object value)\n        {\n            if (IsReadOnly)\n                 throw new NotSupportedException(SR.ReadOnlyInputGesturesCollection);    \n\n            this.Insert(index, value as InputGesture);\n        }\n\n        /// <summary>\n        /// Add\n        /// </summary>\n        /// <param name=\"inputGesture\"></param>\n        int IList.Add(object inputGesture) \n        {\n            if (IsReadOnly)\n                throw new NotSupportedException(SR.ReadOnlyInputGesturesCollection);\n            \n            return this.Add(inputGesture as InputGesture);\n        }\n\n        /// <summary>\n        /// Remove","sourceCodeStart":81,"sourceCodeEnd":117,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/InputGestureCollection.cs#L81-L117","documentation":"InputGestureCollection can be frozen/read-only (e.g. the default gestures of a RoutedCommand). Attempting IList.Insert on a read-only instance throws NotSupportedException with SR.ReadOnlyInputGesturesCollection.","triggerScenarios":"Calling the explicit IList.Insert on a collection whose IsReadOnly is true, typically a RoutedCommand's InputGestures collection.","commonSituations":"Trying to add a gesture directly to an application-level RoutedCommand (e.g. ApplicationCommands.Copy.InputGestures.Insert/Add), which is shared and read-only.","solutions":["Create your own InputGestureCollection and assign via CommandBinding or custom command","Derive a custom RoutedCommand with your own modifiable InputGestures","Check IsReadOnly before mutating"],"exampleFix":"// before\nApplicationCommands.Copy.InputGestures.Insert(0, new KeyGesture(Key.C, ModifierKeys.Control)); // throws\n// after\nvar cmd = new RoutedCommand();\ncmd.InputGestures.Add(new KeyGesture(Key.C, ModifierKeys.Control));","handlingStrategy":"validation","validationCode":"if (!cmd.InputGestures.IsReadOnly)\n    cmd.InputGestures.Insert(0, gesture);\nelse\n    cmd = new RoutedCommand(name, ownerType, new InputGestureCollection { gesture });","typeGuard":null,"tryCatchPattern":"try { gestures.Insert(i, g); }\ncatch (NotSupportedException) { /* collection is frozen; create own command */ }","preventionTips":["Assume ApplicationCommands.* gesture collections are read-only","Define custom RoutedCommands for app-specific shortcuts","Check IsReadOnly before any mutation"],"tags":["wpf","inputgesture","readonly-collection"],"backgroundTag":"unsupported-operation","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"}