Single click edit in WPF DataGrid

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

    I looking for editing cell on single click in MVVM and this is an other way to do it.

    1. Adding behavior in xaml

      
      
          
              
                  
              
          
      
      
    2. The EditCellOnSingleClickBehavior class extend System.Windows.Interactivity.Behavior;

      public class EditCellOnSingleClick : Behavior
      {
          protected override void OnAttached()
          {
              base.OnAttached();
              this.AssociatedObject.LoadingRow += this.OnLoadingRow;
              this.AssociatedObject.UnloadingRow += this.OnUnloading;
          }
      
          protected override void OnDetaching()
          {
              base.OnDetaching();
              this.AssociatedObject.LoadingRow -= this.OnLoadingRow;
              this.AssociatedObject.UnloadingRow -= this.OnUnloading;
          }
      
          private void OnLoadingRow(object sender, DataGridRowEventArgs e)
          {
              e.Row.GotFocus += this.OnGotFocus;
          }
      
          private void OnUnloading(object sender, DataGridRowEventArgs e)
          {
              e.Row.GotFocus -= this.OnGotFocus;
          }
      
          private void OnGotFocus(object sender, RoutedEventArgs e)
          {
              this.AssociatedObject.BeginEdit(e);
          }
      }
      

    Voila !

提交回复
热议问题