Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

wpf

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.

like image 418
HB MAAM Avatar asked Apr 26 '12 08:04

HB MAAM


2 Answers

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>
like image 97
Nicolas Repiquet Avatar answered Oct 11 '22 15:10

Nicolas Repiquet


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

like image 27
Aseem Gautam Avatar answered Oct 11 '22 15:10

Aseem Gautam