dotnet/wpf · error · ArgumentException
SR.InputBinding_ExpectedInputGesture (typeof(KeyGesture))
Error message
SR.InputBinding_ExpectedInputGesture (typeof(KeyGesture))
What it means
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.
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
Example fix
// before keyBinding.Gesture = new MouseGesture(MouseAction.LeftClick); // after keyBinding.Gesture = new KeyGesture(Key.S, ModifierKeys.Control); // or use MouseBinding for mouse gestures
Defensive patterns
Strategy: type-guard
Validate before calling
if (gesture is not KeyGesture) throw new ArgumentException("KeyBinding.Gesture requires a KeyGesture."); Type guard
static bool IsValidKeyBindingGesture(InputGesture g) => g is KeyGesture;
Try / catch
try { keyBinding.Gesture = gesture; } catch (ArgumentException ex) { /* use MouseBinding for MouseGesture */ } Prevention
- Only assign KeyGesture instances to KeyBinding
- Use MouseBinding for MouseGesture values
- Remember KeyBinding.Gesture is typed — the base InputBinding.Gesture accepts any InputGesture
When it happens
Trigger: 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).
Common situations: Copy-pasting a MouseBinding setup into a KeyBinding, or reusing a MouseGesture variable in XAML/code where a KeyGesture is required.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- ' ' is not a Visual or Visual3D.
- 0x80070057
- Animation_AnimationTimelineTypeMismatch
- InvalidEnumArgumentException("action", (int)action…
- InvalidEnumArgumentException("actions", (int)actions…
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/f6bf3d1fddd72ab4.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/KeyBinding.cs:88
[TypeConverter(typeof(KeyGestureConverter))]
[ValueSerializer(typeof(KeyGestureValueSerializer))]
public override InputGesture Gesture
{
get
{
return base.Gesture as KeyGesture;
}
set
{
KeyGesture keyGesture = value as KeyGesture;
if (keyGesture != null)
{
base.Gesture = value;
SynchronizePropertiesFromGesture(keyGesture);
}
else
{
throw new ArgumentException(SR.Format(SR.InputBinding_ExpectedInputGesture, typeof(KeyGesture)));
}
}
}
/// <summary>
/// Dependency Property for Modifiers
/// </summary>
public static readonly DependencyProperty ModifiersProperty =
DependencyProperty.Register("Modifiers", typeof(ModifierKeys), typeof(KeyBinding), new UIPropertyMetadata(ModifierKeys.None, new PropertyChangedCallback(OnModifiersPropertyChanged)));
/// <summary>
/// Modifiers
/// </summary>
public ModifierKeys Modifiers
{
get
{
return (ModifierKeys)GetValue(ModifiersProperty);View on GitHub (pinned to 81131a70a4)