如何将删除绑定到 Caliburn.Micro WPF 中的操作?

How to bind delete to action in Caliburn.Micro WPF?

我目前正在尝试在删除数据网格行时将 "delete" 键绑定到 Action

设置 KeyBinding 似乎不起作用:

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

设置为 DataGrid,也不起作用:

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

我想知道是否有另一种方法可以完成此操作,或者我是否以错误的方式尝试此操作?

最终我希望能够在应用程序中创建自定义键绑定。还有其他方法可以实现吗?

你可以通过从 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>

你网上有很多样例,比如HERE