1

I'm currently trying to bind the "delete" key to an Action when deleting a datagrid row.

Setting a KeyBinding doesn't seem to work:

<DataGrid.InputBindings>
    <KeyBinding Key="Delete"
                cal:Message.Attach="[Key Delete] = [Action DeletePartNumberRow()]"/>
</DataGrid.InputBindings>

Setting it to the DataGrid, doesn't work either:

<DataGrid x:Name="PartNumbers"
          CanUserAddRows="True"
          CanUserDeleteRows="True"
          cal:Message.Attach="[Gesture Delete] = [Action DeletePartNumberRow()]">
...
</DataGrid>

I was wondering if there is another way of accomplishing this, or if I'm attempting this the wrong way?

Ultimately I would like to be able to create custom key bindings in the application. Is there another way that I could accomplish this?

Keith Stein
  • 6,235
  • 4
  • 17
  • 36
Corey
  • 835
  • 1
  • 9
  • 32

1 Answers1

1

You can do it by deriving from System.Windows.Interactivity.TriggerBase :

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

<i:Interaction.Triggers>
    <common:InputBindingTrigger>
        <common:InputBindingTrigger.InputBinding>
            <KeyBinding Key="Delete"/>
        </common:InputBindingTrigger.InputBinding>
        <cal:ActionMessage MethodName="DoTheMagic"/>
    </common:InputBindingTrigger>
</i:Interaction.Triggers>

you have lot of samples on net, for example HERE

Frenchy
  • 16,386
  • 3
  • 16
  • 39
  • Thanks, that did work. Any idea how to setup global custom keyboard shortcuts that can be edited/changed? – Corey May 05 '20 at 16:48
  • what do you mean how to setup? it doesnt exist a global way to do this if its the question...each key to each action...(sorry for my poor english) – Frenchy May 05 '20 at 16:51
  • maybe by looking at Converter...but never test it in this case...https://stackoverflow.com/questions/5016457/dynamically-add-keybindings-in-wpf or https://stackoverflow.com/questions/3625859/can-i-create-a-keybinding-for-a-sequence-of-keys-in-wpf – Frenchy May 05 '20 at 16:55
  • thanks again. I think that will lead me in the right direction. – Corey May 05 '20 at 21:28