Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single click edit in WPF DataGrid

I want the user to be able to put the cell into editing mode and highlight the row the cell is contained in with a single click. By default, this is double click.

How do I override or implement this?

like image 750
Austin Avatar asked Aug 06 '10 18:08

Austin


2 Answers

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);     } } 
like image 192
Micael Bergeron Avatar answered Sep 21 '22 18:09

Micael Bergeron


The answer from Micael Bergeron was a good start for me to find a solution thats working for me. To allow single-click editing also for Cells in the same row thats already in edit mode i had to adjust it a bit. Using SelectionUnit Cell was no option for me.

Instead of using the DataGridCell.Selected Event which is only fired for the first time a row's cell is clicked, i used the DataGridCell.GotFocus Event.

<DataGrid DataGridCell.GotFocus="DataGrid_CellGotFocus" /> 

If you do so you will have always the correct cell focused and in edit mode, but no control in the cell will be focused, this i solved like this

private void DataGrid_CellGotFocus(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);          Control control = GetFirstChildByType<Control>(e.OriginalSource as DataGridCell);         if (control != null)         {             control.Focus();         }     } }  private T GetFirstChildByType<T>(DependencyObject prop) where T : DependencyObject {     for (int i = 0; i < VisualTreeHelper.GetChildrenCount(prop); i++)     {         DependencyObject child = VisualTreeHelper.GetChild((prop), i) as DependencyObject;         if (child == null)             continue;          T castedProp = child as T;         if (castedProp != null)             return castedProp;          castedProp = GetFirstChildByType<T>(child);          if (castedProp != null)             return castedProp;     }     return null; } 
like image 27
user2134678 Avatar answered Sep 20 '22 18:09

user2134678