Double Click Grid Hit Test??

Aug 14, 2009 at 5:42 PM

Is there a way when double clicking to verify that the double click was performed on a row?

The double click event fires when a double click occurs anywhere on the grid. I don't want the edit dialog to pop up when a user double clicks on a column header or empty space. Is there some kind of Hit Test that can be performed?

Aug 17, 2009 at 8:49 AM

You could check to see if SelectedItem!=null, but that isn't optimal (the user could, for example, select a row and then double-click on an empty space, and it will act the same as if the double-click was on the selected row). Also, a not-null check should be performed anyway before doing anything with the SelectedItem property, as there are numerous situations in which it can be null. I, too, would be interested in a better solution.

Aug 17, 2009 at 6:36 PM

I do not know if it is the best way, but here is what I am doing:

 private void myGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)

                FrameworkElement element = myGrid.InputHitTest(e.GetPosition(myGrid)) as FrameworkElement;
                if (element != null)
                    if (element.Parent is DataGridCell)
                        // Do Work here

I have to check if the element.Parent is DataGridCell because the hit test actually returns the TextBlock inside of the cell. I assue this would again get more complicated depending on what was added to the DataGridCell if you were using a Control Template or Data Template, but I haven't gotten that far into my app yet.

So far it seems to work ok, but I just assumed there would be a built in way to tell if the double click was on a record. The grid I have used the most previously was the Dev Express grid for VCL. With those products, there was a hit test that could tell you which portion of the grid you clicked on (Header, cell, etc.).