Passing a command parameter from a Datagrid using key bindings

I have a problem with wpf. I am trying to remove a row from a Datagrid by defining a Keybinding that passes the selected Datagrid row as a command parameter to the command.

This is my key binding:

<UserControl.Resources > <Commands:CommandReference x:Key="deleteKey" Command="{Binding DeleteSelectedCommand}"/> </UserControl.Resources> <UserControl.InputBindings> <KeyBinding Key="D" Modifiers="Control" Command="{StaticResource deleteKey}"/> </UserControl.InputBindings> 

I know this basically works because I can debug to DeleteSelectedCommand. However, an Exception exists because DeleteSelectedCommand expects the Datagrid row to be deleted as a call parameter.

How can I pass SelectedRow via Keybinding?

I want to do this only in XAML, if possible, without changing the code.

+4
source share
3 answers

Instead of trying to use a command parameter, create a property to store the selected line in:

 private Model row; public Model Row { get { return row; } set { if (row != value) { row = value; base.RaisePropertyChanged("Row"); } } } 

where Model is the class of objects displayed by your grid. Add the selectedItem property to the datagrid to use the property:

 <DataGrid SelectedItem="{Binding Row, UpdateSourceTrigger=PropertyChanged}"/> 

then your command goes through the line to the method:

  public ICommand DeleteSelectedCommand { get { return new RelayCommand<string>((s) => DeleteRow(Row)); } } 

and for your key bindings:

  <DataGrid.InputBindings> <KeyBinding Key="Delete" Command="{Binding DeleteSelectedCommand}" /> </DataGrid.InputBindings> 

Hope this helps!

+2
source

If your DataGrid has a name, you can try to configure it this way:

 <KeyBinding Key="D" Modifiers="Control" Command="{StaticResource deleteKey}" CommandParameter="{Binding SelectedItem, ElementName=myDataGrid}"/> 

(Note: CommandParameter only bound in .NET 4 (and, presumably, in future versions), since it has been changed to a dependency property)

+9
source

There is a property in KeyBinding called CommandParameter, everything set here will be passed. However, this is not a dependency property (in 3.5, for 4.0 see HB answer), as you found with Command, the inputbindings are outside the inheritance tree and therefore they did not bother to make them DP.

You will need to use the same solution that you used to bind the team. Here is an example, http://www.wpfmentor.com/2009/01/how-to-add-binding-to-commandparameter.html

  <DataGrid x:Name="grid"> <DataGrid.Resources> <WpfSandBox:DataResource x:Key="cp" BindingTarget="{Binding SelectedItem, ElementName=grid}" /> </DataGrid.Resources> <DataGrid.InputBindings> <KeyBinding Key="q" Modifiers="Control" Command="{x:Static WpfSandBox:Commands.Delete}"> <KeyBinding.CommandParameter> <WpfSandBox:DataResourceBinding DataResource="{StaticResource cp}"/> </KeyBinding.CommandParameter> </KeyBinding> </DataGrid.InputBindings> <DataGrid.Columns> <DataGridTextColumn Binding="{Binding Value}" /> <DataGridTextColumn Binding="{Binding Value}" /> </DataGrid.Columns> </DataGrid> 
0
source

Source: https://habr.com/ru/post/1382281/


All Articles