why is the DataGrid MouseDoubleClick event fired when you double click on the scrollbar?

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

Tags:

C#

.Net

Wpf