Single click edit in WPF DataGrid

Here is how I resolved this issue:

<DataGrid DataGridCell.Selected="DataGridCell_Selected" 
          ItemsSource="{Binding Source={StaticResource itemView}}">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Nom" Binding="{Binding Path=Name}"/>
        <DataGridTextColumn Header="Age" Binding="{Binding Path=Age}"/>
    </DataGrid.Columns>
</DataGrid>

This DataGrid is bound to a CollectionViewSource (Containing dummy Person objects).

The magic happens there : DataGridCell.Selected=”DataGridCell_Selected”.

I simply hook the Selected Event of the DataGrid cell, and call BeginEdit() on the DataGrid.

Here is the code behind for the event handler :

private void DataGridCell_Selected(object sender, RoutedEventArgs e)
{
    // Lookup for the source to be DataGridCell
    if (e.OriginalSource.GetType() == typeof(DataGridCell))
    {
        // Starts the Edit on the row;
        DataGrid grd = (DataGrid)sender;
        grd.BeginEdit(e);
    }
}

Leave a Comment