Wpf Datagrid doubleclick event

Mar 19, 2010 at 6:58 PM

I am using the code listed on http://www.scottlogic.co.uk/blog/colin/2008/12/wpf-datagrid-detecting-clicked-cell-and-row/ for getting row value from datagrid, but I would like the same thing happening with mouse doubleclick event, not with mouse right click event.

the dep value after execution of this statement “DependencyObject dep = (DependencyObject)e.OriginalSource;” shows ’system.windows.controls.textblock’ for both events that when I doubleclick the row and when I right click the row

But after execution of following statement:

dep = VisualTreeHelper.GetParent(dep);

dep value is ‘null for mouse doubleclick event and

dep value is presenter(it’s parent element) for mouse right click event.

Can anyone help me? Thanks.

Mar 20, 2010 at 5:43 AM

This is what I'm doing:


Public Class SearchDataGrid
    Inherits DataGrid

    Public Event ItemOpened As EventHandler(Of ItemOpenedEventArgs)

    Private Sub SearchDataGrid_PreviewKeyDown(ByVal sender As Object, ByVal e As System.Windows.Input.KeyEventArgs) Handles Me.PreviewKeyDown
        If e.Key = Input.Key.Enter Then
            If Me.SelectedItem IsNot Nothing Then
                RaiseEvent ItemOpened(sender, New ItemOpenedEventArgs(Me.SelectedItem))
                e.Handled = True
            End If
        End If
    End Sub

    Private Sub SearchDataGrid_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles Me.MouseDoubleClick
        Dim node As DependencyObject = CType(e.OriginalSource, DependencyObject)
        Do Until TypeOf node Is Microsoft.Windows.Controls.DataGridRow OrElse node Is Nothing
            node = VisualTreeHelper.GetParent(node)

        If node IsNot Nothing Then
            Dim data = (CType(node, Microsoft.Windows.Controls.DataGridRow)).DataContext
            If data IsNot Nothing Then
                RaiseEvent ItemOpened(sender, New ItemOpenedEventArgs(data))
                e.Handled = True
            End If
        End If

    End Sub

End Class