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..