2

I'm trying to create a DataGrid having the following features:

  • Readonly Datagrid, but provide editing capabilities through double click and separate edit form (double click on specific row)
  • ContextMenu which calls new/edit/delete form (right click on whole DataGrid)
  • Delete key which calls delete form (on specific selected row)

I thought it would be a good idea to use ICommand, so I created a DataGrid like this:

public class MyDataGrid : DataGrid {
    public static readonly RoutedCommand NewEntry = new RoutedCommand();
    public static readonly RoutedCommand EditEntry = new RoutedCommand();
    public static readonly RoutedCommand DeleteEntry = new RoutedCommand();

    public MyDataGrid() {
        CommandBindings.Add(new CommandBinding(NewEntry, ..., ...));
        CommandBindings.Add(new CommandBinding(EditEntry, ..., ...));
        CommandBindings.Add(new CommandBinding(DeleteEntry, ..., ...));

        InputBindings.Add(new InputBinding(DeleteCommand, new KeyGesture(Key.Delete)));
        InputBindings.Add(new MouseBinding(EditEntry, new MouseGesture(MouseAction.LeftDoubleClick)));

        // ContextMenu..working fine
    }
}

I then realized, double-clicking a row isn't working, so I added this:

LoadingRow += (s, e) =>
    e.Row.InputBindings.Add(new MouseBinding(EditEntry,
        new MouseGesture(MouseAction.LeftDoubleClick)));

And of course the delete key isn't working either, I added this:

PreviewKeyDown += (s, e) => { if(e.Key == Key.Delete) { ... } };

Why do I have to do that? Isn't the whole point of having Commands to prevent this kind of hacking around with events? Do I miss something?

In my simple and perfect world, I want to decide in the CanExecute method whether it is appropriate to handle the command, and not subscribe to tons of different event handlers..

duedl0r
  • 9,289
  • 3
  • 30
  • 45

1 Answers1

1

Usually I attach the command to the DataGridCell using a Style

Here's an example using a custom AttachedCommandBehavior

<Style TargetType="{x:Type DataGridCell}">
    <Setter Property="my:CommandBehavior.Command" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:MyView}}, Path=DataContext.ShowPopupCommand}" />
    <Setter Property="my:CommandBehavior.CommandParameter" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}, Path=DataContext}" />
    <Setter Property="my:CommandBehavior.Event" Value="MouseDoubleClick" />
</Style>

I can't remember why I attached it to the Cell instead of the Row, but I'm sure there was a reason. You could try attaching the event to the Row instead and see what happens.

Rachel
  • 130,264
  • 66
  • 304
  • 490