سؤال

I've a wpf specific problem. I'm trying to delete a Row from a Datagrid, by defining a Keybinding that passes the selected Row of the Datagrid as a Commandparameter to a Command.

This is my Keybinding:

<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 up to the DeleteSelectedCommand. However there flies an Exception because the DeleteSelectedCommand expectes a Row of the Datagrid to delete as Call Parameter.

How can I pass the SelectedRow through the Keybinding?

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

هل كانت مفيدة؟

المحلول

Rather than trying to use a command parameter, create a property to store the selected row 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 the objects your grid is displaying. Add the selectedItem property on the datagrid to use the property:

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

then have your command pass through the row to the method:

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

and for your keybindings:

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

Hope that helps!

نصائح أخرى

If your DataGrid has a name you can try to target it that way:

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

(Note: CommandParameter is only bindable in .NET 4 (and presumably the following versions) as it was changed into a dependency property)

There is a property called CommandParameter on KeyBinding, anything set here will be passed through. It however is not a dependency property (in 3.5, for 4.0 see H.B.'s answer) as you have found with the Command, inputbindings are out of the inheritance tree and so they didn't bother making them DP's.

You will need to use the same solution you used for binding the command. There's an example here, 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>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top