Single click edit in WPF DataGrid

前端 未结 11 1560
野的像风
野的像风 2020-11-27 10:47

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.

H

11条回答
  •  抹茶落季
    2020-11-27 11:00

    Here is how I resolved this issue:

    
        
            
            
        
    
    

    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);
        }
    }
    

提交回复
热议问题