How to capture double click event in Data Grid row using WPF and C#

There are a number of name differences between a windows form and a WPF program. For example, in a windows form program the event which is triggered when a user double-clicks on a data grid is called Doubleclick. The event in a WPF program is called MouseDoubleClick. Therefore the way to implement, program or add functionality to a WPF program when a user double clicks on a DataGrid is by adding a MouseDoubleClick event to the DataGrid as shown below.

<DataGrid AutoGenerateColumns="True" Height="200"
             Margin="12,49,12,0"
             Name="dataGrid1" IsReadOnly="True"
             VerticalAlignment="Top"
             MouseDoubleClick="dataGrid1_MouseDoubleClick"/>

And the code behind would look something like the below.

private void dataGrid1_MouseDoubleClick
                          (object sender, MouseButtonEventArgs e)
{
     int row = dataGrid1.SelectedIndex;
     label1.Content = "Row " + row.ToString() + " was selected.";
}

It wasn’t hard to figure this out, it just took a few minutes to find the WPF name for the double click event. Coming from a forms and asp.net background, I found it worthy of a blog.

Download the source