{"record":{"id":"f6bf3d1fddd72ab4","repo":"dotnet/wpf","slug":"sr-inputbinding-expectedinputgesture-typeof-keygesture","errorCode":null,"errorMessage":"SR.InputBinding_ExpectedInputGesture (typeof(KeyGesture))","messagePattern":"SR\\.InputBinding_ExpectedInputGesture \\(typeof\\(KeyGesture\\)\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/KeyBinding.cs","lineNumber":88,"sourceCode":"        [TypeConverter(typeof(KeyGestureConverter))]\n        [ValueSerializer(typeof(KeyGestureValueSerializer))]\n        public override InputGesture Gesture\n        {\n            get\n            {\n                return base.Gesture as KeyGesture;\n            }\n            set\n            {\n                KeyGesture keyGesture = value as KeyGesture;\n                if (keyGesture != null)\n                {\n                    base.Gesture = value;\n                    SynchronizePropertiesFromGesture(keyGesture);\n                }\n                else\n                {\n                    throw new ArgumentException(SR.Format(SR.InputBinding_ExpectedInputGesture, typeof(KeyGesture)));\n                }\n            }\n        }\n\n        /// <summary>\n        ///     Dependency Property for Modifiers\n        /// </summary>\n        public static readonly DependencyProperty ModifiersProperty =\n            DependencyProperty.Register(\"Modifiers\", typeof(ModifierKeys), typeof(KeyBinding), new UIPropertyMetadata(ModifierKeys.None, new PropertyChangedCallback(OnModifiersPropertyChanged)));\n\n        /// <summary>\n        ///     Modifiers\n        /// </summary>\n        public ModifierKeys Modifiers\n        {\n            get\n            {\n                return (ModifierKeys)GetValue(ModifiersProperty);","sourceCodeStart":70,"sourceCodeEnd":106,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/KeyBinding.cs#L70-L106","documentation":"KeyBinding.Gesture (set) only accepts a KeyGesture; assigning a different InputGesture-derived type (e.g. MouseGesture) throws ArgumentException with SR.InputBinding_ExpectedInputGesture(typeof(KeyGesture)). KeyBinding is a typed specialization of InputBinding, so the gesture type must match.","triggerScenarios":"Assigning KeyBinding.Gesture a MouseGesture, or using the KeyBinding(object command, InputGesture gesture) constructor path where the gesture is not a KeyGesture (the setter is shared).","commonSituations":"Copy-pasting a MouseBinding setup into a KeyBinding, or reusing a MouseGesture variable in XAML/code where a KeyGesture is required.","solutions":["Pass a KeyGesture (e.g. new KeyGesture(Key.S, ModifierKeys.Control)) to KeyBinding.Gesture or its constructor","Use MouseBinding instead of KeyBinding when the gesture is a MouseGesture","Guard with 'is KeyGesture' before assigning"],"exampleFix":"// before\nkeyBinding.Gesture = new MouseGesture(MouseAction.LeftClick);\n// after\nkeyBinding.Gesture = new KeyGesture(Key.S, ModifierKeys.Control);\n// or use MouseBinding for mouse gestures","handlingStrategy":"type-guard","validationCode":"if (gesture is not KeyGesture) throw new ArgumentException(\"KeyBinding.Gesture requires a KeyGesture.\");","typeGuard":"static bool IsValidKeyBindingGesture(InputGesture g) => g is KeyGesture;","tryCatchPattern":"try { keyBinding.Gesture = gesture; } catch (ArgumentException ex) { /* use MouseBinding for MouseGesture */ }","preventionTips":["Only assign KeyGesture instances to KeyBinding","Use MouseBinding for MouseGesture values","Remember KeyBinding.Gesture is typed — the base InputBinding.Gesture accepts any InputGesture"],"tags":["wpf","input","keybinding","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"}