Why is the DataGrid MouseDoubleClick event fired when i double click on the scrollbar or on the header?
Is there any way to avoid this and fire the event only when i double clicked inside the datagrid.
Scrollbar and header are part of the grid but do not handle double click, so the event "bubbles" up to the grid.
The inelegant solution is to somewhat find out "what was clicked" by the mean of event source or mouse coordinates.
But you can also do something like that (untested):
<DataGrid>
<DataGrid.RowStyle>
<Style TargetType="{x:Type DataGridRow}">
<EventSetter Event="MouseDoubleClick" Handler="OnRowDoubleClicked"/>
</Style>
</DataGrid.RowStyle>
</DataGrid>
You can check the details about the hit-point, inside the mouse click event -
DependencyObject dep = (DependencyObject)e.OriginalSource;
// iteratively traverse the visual tree
while ((dep != null) &&
!(dep is DataGridCell) &&
!(dep is DataGridColumnHeader))
{
dep = VisualTreeHelper.GetParent(dep);
}
if (dep == null)
return;
if (dep is DataGridColumnHeader)
{
DataGridColumnHeader columnHeader = dep as DataGridColumnHeader;
// do something
}
if (dep is DataGridCell)
{
DataGridCell cell = dep as DataGridCell;
// do something
}
https://blog.scottlogic.com/2008/12/02/wpf-datagrid-detecting-clicked-cell-and-row.html
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