{"record":{"id":"bf1f403ecac2cd90","repo":"dotnet/wpf","slug":"sr-inputbinding-expectedinputgesture-typeof-mousegesture","errorCode":null,"errorMessage":"SR.InputBinding_ExpectedInputGesture (typeof(MouseGesture))","messagePattern":"SR\\.InputBinding_ExpectedInputGesture \\(typeof\\(MouseGesture\\)\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/MouseBinding.cs","lineNumber":101,"sourceCode":"                return base.Gesture as MouseGesture;\n            }\n            set\n            {\n                MouseGesture oldMouseGesture = Gesture as MouseGesture;\n                MouseGesture mouseGesture = value as MouseGesture;\n                if (mouseGesture != null)\n                {\n                     base.Gesture  = mouseGesture;\n                     SynchronizePropertiesFromGesture(mouseGesture);\n                     if (oldMouseGesture != mouseGesture)\n                     {\n                         oldMouseGesture?.PropertyChanged -= new PropertyChangedEventHandler(OnMouseGesturePropertyChanged);\n                         mouseGesture.PropertyChanged += new PropertyChangedEventHandler(OnMouseGesturePropertyChanged);\n                     }\n                }\n                else\n                {\n                    throw new ArgumentException(SR.Format(SR.InputBinding_ExpectedInputGesture, typeof(MouseGesture)));\n                }\n             }\n        }\n\n        /// <summary>\n        ///     Dependency property for MouseAction\n        /// </summary>\n        public static readonly DependencyProperty MouseActionProperty =\n            DependencyProperty.Register(\"MouseAction\", typeof(MouseAction), typeof(MouseBinding), new UIPropertyMetadata(MouseAction.None, new PropertyChangedCallback(OnMouseActionPropertyChanged)));\n\n        /// <summary>\n        ///     MouseAction\n        /// </summary>\n        public MouseAction MouseAction\n        {\n            get\n            {\n                return (MouseAction)GetValue(MouseActionProperty);","sourceCodeStart":83,"sourceCodeEnd":119,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/MouseBinding.cs#L83-L119","documentation":"MouseBinding.Constructor verifies that the gesture passed in is a MouseGesture; if not, it throws ArgumentException with SR.InputBinding_ExpectedInputGesture naming the expected type. InputBinding only accepts gestures of the matching concrete type, so a KeyGesture (or any other InputGesture) cannot be assigned to a MouseBinding.","triggerScenarios":"new MouseBinding(command, gesture) or setting MouseBinding.Gesture where gesture is an InputGesture that is not a MouseGesture, e.g. a KeyGesture created for keyboard shortcuts.","commonSituations":"Developers reuse a shortcut-definition helper that returns KeyGesture for all bindings, or switch from KeyBinding to MouseBinding without changing the gesture type.","solutions":["Create a MouseGesture (e.g. new MouseGesture(MouseAction.LeftClick, ModifierKeys.Control)) instead of a KeyGesture.","Use KeyBinding for keyboard-based gestures and MouseBinding only for mouse actions.","If the gesture source is dynamic, check `gesture is MouseGesture` before constructing the binding, or cast with a fallback."],"exampleFix":"// before\nvar binding = new MouseBinding(myCommand, new KeyGesture(Key.S, ModifierKeys.Control));\n// after\nvar binding = new MouseBinding(myCommand,\n    new MouseGesture(MouseAction.LeftClick, ModifierKeys.Control));","handlingStrategy":"type-guard","validationCode":"if (gesture is MouseGesture mg) { var binding = new MouseBinding(command, mg); }","typeGuard":"static bool IsMouseGesture(InputGesture g) => g is MouseGesture;","tryCatchPattern":"try { var binding = new MouseBinding(command, gesture); } catch (ArgumentException ex) { /* log: gesture must be MouseGesture */ }","preventionTips":["Use MouseGesture for MouseBinding and KeyGesture for KeyBinding","Centralize gesture creation in one factory method","Check `is MouseGesture` before any dynamic binding construction"],"tags":["wpf","input-binding","argument-exception"],"backgroundTag":"invalid-argument-value","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"}