How do i handle cell double click event on WPF DataGrid, equivalent to windows DataGrid's Events?

iVad picture iVad · Jan 28, 2013 · Viewed 24.8k times · Source

As you know, in windows C#'s gridview, if we want to handle a click/double click event on cell then there are events like CellClick, CellDoubleClick, etc.

So, i wanna do same like as windows gridview with WPF DataGrid. I have searched so far but neither answer is applicable nor useful. Some of them says use the MouseDoubleClick event but, in this event, we have to check for each row as well as item in that row, so it is time consuming to check every cell for data and timing is most important here.

My DataGrid is bounded to DataTable and AutoGeneratedColumn is False. If your answer is based on AutoGeneratedColumn=True then it is not possible. Even, i 'm changing the styles of datagrid cell according to data, so there is no way to change AutoGeneratedColumn property.

A Cell Clicking/Double Clicking event should be as faster as windows grid's event. If it is possible then tell me how, and if not, then what is the alternative to do it?

Please Help Me.....

Thanks a lot....

Answer

Matthew S picture Matthew S · Nov 10, 2017

I know this may be a little late to the party, but this might be useful to someone else down the road.

In your MyView.xaml:

<DataGrid x:Name="MyDataGrid" ...>
    <DataGrid.Resources>
        <Style TargetType="{x:Type DataGridCell}">
            <EventSetter Event="MouseDoubleClick" Handler="DataGridCell_MouseDoubleClick"/>
        </Style>
    </DataGrid.Resources>

    <!-- TODO: The rest of your DataGrid -->
</DataGrid>

In your MyView.xaml.cs:

private void DataGridCell_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    var dataGridCellTarget = (DataGridCell)sender;
    // TODO: Your logic here
}