Single click edit in WPF DataGrid

前端 未结 11 1591
野的像风
野的像风 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:07

    I know that I am a bit late to the party but I had the same problem and came up with a different solution:

         public class DataGridTextBoxColumn : DataGridBoundColumn
     {
      public DataGridTextBoxColumn():base()
      {
      }
    
      protected override FrameworkElement GenerateEditingElement(DataGridCell cell, object dataItem)
      {
       throw new NotImplementedException("Should not be used.");
      }
    
      protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
      {
       var control = new TextBox();
       control.Style = (Style)Application.Current.TryFindResource("textBoxStyle");
       control.FontSize = 14;
       control.VerticalContentAlignment = VerticalAlignment.Center;
       BindingOperations.SetBinding(control, TextBox.TextProperty, Binding);
        control.IsReadOnly = IsReadOnly;
       return control;
      }
     }
    
            
            
                
                
                
                
            
        
    

    As you can see I wrote my own DataGridTextColumn inheriting everything vom the DataGridBoundColumn. By overriding the GenerateElement Method and returning a Textbox control right there the Method for generating the Editing Element never gets called. In a different project I used this to implement a Datepicker column, so this should work for checkboxes and comboboxes as well.

    This does not seem to impact the rest of the datagrids behaviours..At least I haven't noticed any side effects nor have I got any negative feedback so far.

提交回复
热议问题