Implement ctrl+shift+MouseOver on a button in mvvm

457 Views Asked by At

I am trying to implement ctrl+shift+mouseover on a button in wpf, mvvm. I am doing something like:

<Button.InputBindings>
    <KeyBinding Gesture="Ctrl+Shift" Command="{Binding KeyCommand}" 
CommandParameter="{Binding Mode=OneWay, RelativeSource={RelativeSource Self}}" 
/>
</Button.InputBindings>

or something like:

<i:Interaction.Triggers>
    <i:EventTrigger EventName="MouseEnter">
      <i:InvokeCommandAction Command="{Binding MouseOverCommand}" />
    </i:EventTrigger>
 </i:Interaction.Triggers>

I am unable to get the required behavior as Mouse Gesture does not support "MouseEnter" event. I was trying to implement in a way like:

Step 1.- Through keybinding setting a variable in the viewmodel.

Step 2.- Accessing that variable's value on MouseEnter and do whatever in the command.

Any help is appreciated.

1

There are 1 best solutions below

3
mm8 On

You could implement an attached behaviour that hooks up event handlers to the MouseEnter and MouseLeftButtonDown events and check whether the Ctrl and Shift keys are pressed by the time the events are raised, e.g.:

private void OnMouseEnter(object sender, MouseEventArgs e)
{
    if ((Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))
        && (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)))
    {
        TheCommand.Execute(null);
    }
}