I have a DataGrid in WPF. When I double-click on a row, a query to a database should be executed.
This DataGrid has horizontal and vertical scroll bars, and I notice that when I click quickly on the arrow button of one of the scroll bars, it sends the query to the database.
The problem is that I am using the DataGrid's MouseDoubleClick
event, so the scroll bars belong the the DataGrid, and when they are double-clicked, this event is raised.
Is there any way to execute the double click event only when I double click on a row of the DataGrid and not when I double-click on part of the scroll bars?
In your MouseDoubleClick event try doing 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..
}
}
Yes, register the event in RowStyle
.
<DataGrid.RowStyle>
<Style TargetType="{x:Type DataGridRow}">
<EventSetter Event="PreviewMouseDoubleClick" Handler="Row_PreviewMouseDoubleClick" />
</Style>
</DataGrid.RowStyle>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With