How to bind a Command to double-click on a row in DataGrid?

Up vote 3 down vote favorite 1 share g+ share fb share tw.

I have developed a WPF UserControl that is intended to be used as a pick list as follows: A DataGrid bound to a CollectionView of entities (e.g. Of Employees) A TextBox above the DataGrid that can be used to filter items displayed in the DataGrid. I want to expose a Command that will be executed when the user double-clicks on a row in the DataGrid. The container can then react to this by doing something with the SelectedItem in the DataGrid.

So far I've tried to handle the double-click as follows: ... However the double-click event still fires when the user clicks in the DataGrid header. I'd like to be able to limit it so that the Command is only executed when the double click is in the body of the DataGrid. Is there a declarative way to do this?

UPDATE I'm just getting to grips with WPF and MVVM, and am really looking for guidance on how to implement low-level reusable components like this. Any general advice will also be gratefully received and upvoted. As it stands, I'm assuming I will want this UserControl to: Expose a dependency property "SelectedItem" that is bound to the DataGrid's SelectedItem Expose a RoutedEvent "ItemDoubleClick" or similar that is fired when the user double-clicks on a row.

Implement ICommandSource and call CommandHelpers. ExecuteCommandSource(this) from the row double-click event handler. Wpf datagrid icommand link|improve this question edited Apr 28 '11 at 6:40 asked Apr 27 '11 at 18:14Joe29.8k53281 72% accept rate.

If code behind is not a problem: private void Row_Loaded(object sender, RoutedEventArgs e) { var row = sender as DataGridRow; row.InputBindings. Add(new MouseBinding(MyCommands. MyCommand, new MouseGesture() { MouseAction = MouseAction.

LeftDoubleClick })); }.

1 thanks - this is exactly what I want to achieve, but I was wondering if there is a declarative way to do it. – Joe Apr 28 '11 at 5:20 I doubt that there is an easy way; you can pipe events to commands using Interactivity from the Blend SDK and a EventToCommandAction, the problem is that using Interactivity in a style is very complicated, but if you do not mind the trouble there are ways. – H.B. Apr 28 '11 at 5:31 @HB I'm just getting to grips with MVVM and want to KISS for now, so I think I'll go for a codebehind solution.

One remark about your solution - it's presumably creating a MouseBinding for every row as it's added to the DataGrid - could this be slightly inefficient if there are a large number of rows? If I'm going the codebehind route, I wonder whether it would be more efficient to handle the DataGrid. MouseDoubleClick event, hit test to make sure I'm in a row, then execute the command.

– Joe Apr 28 '11 at 6:34 I doubt that it is a problem because by default the DataGrid should be virtualizing, i.e. Only the controls for the displayed rows are actually generated, so depending on the size of your view/window there should not be much more than one or two hundred instantiated rows at a time. (You need to populate it via ItemsSource of course.

) You could add a breakpoint in that handler once the window is loaded, if you scroll the grid the handler should be fired as new rows are created. – H.B. Apr 28 '11 at 7:20.

You can simply put the DataGrid into a Grid and define your InputBindings in the Grid. In the canExecute-definition, you should check, if a row is selected. That works for the KeyBinding as well, for example a custom Delete-Command.

...

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions