Как связать клавишу удаления клавиатуры с помощью MVVM - PullRequest
0 голосов
/ 30 мая 2018

У меня есть команда relay в viewmodel.

 DeleteCommand = new RelayCommand<Customer>(param =>
  {
 //my code
  });

Я привязываю эту команду к кнопке и хочу связать ее для удаления ключа.

<KeyBinding  Key="Delete"
        Command="{Binding DeleteCommand}" CommandParameter="{Binding SelectedItem, ElementName=CustomerListView}"/>

Это работа надКнопка, но не работает с клавишей удаления.

<DataGrid Margin="10" CanUserDeleteRows="False" CellStyle="{StaticResource MaterialDesignDataGridCell}" Height="310" Name="CustomerListView"  filter:DataGridExtensions.UseBackgroundWorkerForFiltering="True"
    filter:DataGridExtensions.IsClearButtonVisible="False" IsReadOnly="True"
   filter:DataGridExtensions.IsFilterVisible="True"
   ColumnHeaderStyle="{StaticResource {ComponentResourceKey 
    TypeInTargetAssembly={x:Type filter:DataGridHeaderFilterControl}, 
    ResourceId=DataGridHeaderFilterControlStyle}}"
    AutoGenerateColumns="False"
    ItemsSource="{Binding Path=CustomerCollection}" >
     <DataGrid.Columns>
      <DataGridTextColumn  Width="100" Header="Code" Binding="{Binding Path=Code}"/>
      <DataGridTextColumn Header="Name" Binding="{Binding Path=Name}" Width="200"/>
      </DataGrid.Columns>
   <i:Interaction.Triggers>
   <i:EventTrigger EventName="MouseDoubleClick">
    <command:EventToCommand Command="{Binding EditCommand}" CommandParameter="{Binding SelectedItem, ElementName=CustomerListView}" />
   </i:EventTrigger>
  </i:Interaction.Triggers>
</DataGrid>

1 Ответ

0 голосов
/ 30 мая 2018

Легко, как пирог:

<Window.InputBindings>
    <KeyBinding Key="Delete" Command="{Binding DeleteCommand}"/>
</Window.InputBindings>

Для получения дополнительной информации смотрите ЗДЕСЬ

...