5

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?

2 Answers 2

8

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..
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

I think this will not work. There are many ContentPresenter in DataGrid (not only for rows) and you can't be sure that a ContentPresenter is a direct Parent of e.OriginalSource.
I have used this approach with success before. The point is that if the direct Parent of e.OriginalSource is a ContentPresenter you can assume the user clicked on a row or an object in one of the columns of that row and thus you can get the selected row. If it is something else, like a Border or Grid, just ignore it. Do you have any examples where this could go wrong?
I havn't tried. This was my thinking only. I mean a ContentPresenter is not an indication for a row. Does that really not fire on ColumnHeaders and ScrollBars? And what if Template isn't default?
The Parent of a ColumnHeader is Grid and ScrollBar is either RepeatButton or Thumb. I guess you could probably edit the Template to produce undesired results with this method, but I'm sure you could just adjust it accordingly? I haven't been able to make it fail yet, but I don't know if this way of doing it is bullet proof. If it isn't I would like to know :)
ContentPresenter Don't Work (There are many ContentPresenter in DataGrid es: Headers)
|
7

Yes, register the event in RowStyle.

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

3 Comments

What is the overhead of adding an eventsetter to every row in the data grid?
I think nothing to speak of because there is only one handler instance for all rows.
Well done! Works great and I see no performance degradation (in regards what Mark asked).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.