Only execute the WPF MouseDoubleClick event when you click on the DataGrid row

I have a DataGrid in WPF. When I double click on a row, the database query should be executed.

This DataGrid has horizontal and vertical scroll bars, and I notice that when I quickly click the arrow button of one of the scroll bars, it sends a query to the database.

The problem is that I am using the DataGrid MouseDoubleClick event, so the scrollbars belong to the DataGrid, and when I double-click this event increases.

Is there a way to execute a double-click event only by double-clicking on the DataGrid row, and not double-clicking on part of the scroll bars?

+4
source share
2 answers

In your MouseDoubleClick event, try to do this:

 private void DataGridMouseDoubleClick(object sender, MouseButtonEventArgs e) { DependencyObject src = VisualTreeHelper.GetParent((DependencyObject)e.OriginalSource); // Checks if the user double clicked on a row in the datagrid [ContentPresenter] if (src.GetType() == typeof(ContentPresenter)) { // Your logic.. } } 
+8
source

Yes, register an event in RowStyle .

 <DataGrid.RowStyle> <Style TargetType="{x:Type DataGridRow}"> <EventSetter Event="PreviewMouseDoubleClick" Handler="Row_PreviewMouseDoubleClick" /> </Style> </DataGrid.RowStyle> 
+6
source

All Articles