dotnet/wpf · error · ArgumentException

SR.InputBinding_ExpectedInputGesture (typeof(MouseGesture))

Error message

SR.InputBinding_ExpectedInputGesture (typeof(MouseGesture))

What it means

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.

Solutions

  1. Create a MouseGesture (e.g. new MouseGesture(MouseAction.LeftClick, ModifierKeys.Control)) instead of a KeyGesture.
  2. Use KeyBinding for keyboard-based gestures and MouseBinding only for mouse actions.
  3. If the gesture source is dynamic, check `gesture is MouseGesture` before constructing the binding, or cast with a fallback.

Example fix

// before
var binding = new MouseBinding(myCommand, new KeyGesture(Key.S, ModifierKeys.Control));
// after
var binding = new MouseBinding(myCommand,
    new MouseGesture(MouseAction.LeftClick, ModifierKeys.Control));
Defensive patterns

Strategy: type-guard

Validate before calling

if (gesture is MouseGesture mg) { var binding = new MouseBinding(command, mg); }

Type guard

static bool IsMouseGesture(InputGesture g) => g is MouseGesture;

Try / catch

try { var binding = new MouseBinding(command, gesture); } catch (ArgumentException ex) { /* log: gesture must be MouseGesture */ }

Prevention

When it happens

Trigger: 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.

Common situations: Developers reuse a shortcut-definition helper that returns KeyGesture for all bindings, or switch from KeyBinding to MouseBinding without changing the gesture type.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/bf1f403ecac2cd90. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/MouseBinding.cs:101

                return base.Gesture as MouseGesture;
            }
            set
            {
                MouseGesture oldMouseGesture = Gesture as MouseGesture;
                MouseGesture mouseGesture = value as MouseGesture;
                if (mouseGesture != null)
                {
                     base.Gesture  = mouseGesture;
                     SynchronizePropertiesFromGesture(mouseGesture);
                     if (oldMouseGesture != mouseGesture)
                     {
                         oldMouseGesture?.PropertyChanged -= new PropertyChangedEventHandler(OnMouseGesturePropertyChanged);
                         mouseGesture.PropertyChanged += new PropertyChangedEventHandler(OnMouseGesturePropertyChanged);
                     }
                }
                else
                {
                    throw new ArgumentException(SR.Format(SR.InputBinding_ExpectedInputGesture, typeof(MouseGesture)));
                }
             }
        }

        /// <summary>
        ///     Dependency property for MouseAction
        /// </summary>
        public static readonly DependencyProperty MouseActionProperty =
            DependencyProperty.Register("MouseAction", typeof(MouseAction), typeof(MouseBinding), new UIPropertyMetadata(MouseAction.None, new PropertyChangedCallback(OnMouseActionPropertyChanged)));

        /// <summary>
        ///     MouseAction
        /// </summary>
        public MouseAction MouseAction
        {
            get
            {
                return (MouseAction)GetValue(MouseActionProperty);

View on GitHub (pinned to 81131a70a4)