Quellcode durchsuchen

Added cancelling items on directedit "Escape" key pressed

Kenric Nugteren vor 8 Monaten
Ursprung
Commit
e0451876b0

+ 0 - 21
inabox.wpf/DynamicGrid/DynamicGrid.cs

@@ -2409,27 +2409,6 @@ public class DynamicGridCellStyle : DynamicGridStyle<Control>
     public override DependencyProperty ForegroundProperty => Control.ForegroundProperty;
 }
 
-public class GridSelectionControllerExt : GridSelectionController
-{
-    public GridSelectionControllerExt(SfDataGrid datagrid)
-        : base(datagrid)
-    {
-    }
-
-    protected override void ProcessSelectedItemChanged(SelectionPropertyChangedHandlerArgs handle)
-    {
-        base.ProcessSelectedItemChanged(handle);
-        if (handle.NewValue != null)
-        {
-            //this.DataGrid.ScrollInView(this.CurrentCellManager.CurrentRowColumnIndex);
-            //int rowIndex = this.CurrentCellManager.CurrentRowColumnIndex.RowIndex;
-            var columnIndex = CurrentCellManager.CurrentRowColumnIndex.ColumnIndex;
-            var scrollRowIndex = DataGrid.GetVisualContainer().ScrollRows.LastBodyVisibleLineIndex;
-            DataGrid.ScrollInView(new RowColumnIndex(scrollRowIndex, columnIndex));
-        }
-    }
-}
-
 // Used to render boolean columns (the default "false" value shows what appears to be an intermediate state, which is ugly
 // This should show nothing for false, and a tick in a box for true
 public class BoolToImageConverter : AbstractConverter<bool,ImageSource>

+ 59 - 1
inabox.wpf/DynamicGrid/UIComponent/DynamicGridGridUIComponent.cs

@@ -188,7 +188,7 @@ public class DynamicGridGridUIComponent<T> : IDynamicGridUIComponent<T>, IDynami
         DataGrid.MouseRightButtonUp += DataGrid_MouseRightButtonUp;
         DataGrid.KeyUp += DataGrid_KeyUp;
         DataGrid.PreviewGotKeyboardFocus += DataGrid_PreviewGotKeyboardFocus;
-        //DataGrid.SelectionController = new GridSelectionControllerExt(DataGrid);
+        DataGrid.SelectionController = new GridSelectionControllerExt(DataGrid, this);
         
         DataGrid.SetValue(ScrollViewer.VerticalScrollBarVisibilityProperty, ScrollBarVisibility.Visible);
         
@@ -212,6 +212,30 @@ public class DynamicGridGridUIComponent<T> : IDynamicGridUIComponent<T>, IDynami
         //DataGrid.CellRenderers.Add("TextBox", new CustomTextCellRenderer(this));
     }
 
+    public class GridSelectionControllerExt : GridSelectionController
+    {
+        private DynamicGridGridUIComponent<T> Grid;
+
+        public GridSelectionControllerExt(SfDataGrid datagrid, DynamicGridGridUIComponent<T> grid)
+            : base(datagrid)
+        {
+            Grid = grid;
+        }
+
+        public override bool HandleKeyDown(KeyEventArgs args)
+        {
+            if (args.Key == Key.Escape)
+            {
+                Grid.CancelEdit();
+                return false;
+            }
+            else
+            {
+                return base.HandleKeyDown(args);
+            }
+        }
+    }
+
     //private class CustomTextCellRenderer : GridCellTextBoxRenderer
     //{
     //    private DynamicGridGridUIComponent<T> Component;
@@ -1434,6 +1458,21 @@ public class DynamicGridGridUIComponent<T> : IDynamicGridUIComponent<T>, IDynami
         }
     }
 
+    void UpdateRow(CoreRow row, DataRow dataRow)
+    {
+        foreach(var (key, value) in row)
+        {
+            var datacolname = key.Replace(".", "_");
+            var dataValue = dataRow[datacolname];
+            if (!Equals(dataValue, value) && !(value is null && dataValue == DBNull.Value))
+            {
+                dataRow[datacolname] = value ?? DBNull.Value;
+            }
+        }
+        for (var i = 0; i < ActionColumns.Count; i++)
+            dataRow[$"ActionColumn{i}"] = ActionColumns[i].Data(row);
+    }
+
     void IDynamicGridUIComponent<T>.UpdateRow(CoreRow row)
     {
         var dataRow = GetDataRow(row);
@@ -1567,6 +1606,18 @@ public class DynamicGridGridUIComponent<T> : IDynamicGridUIComponent<T>, IDynami
             bChanged = true;
     }
 
+    private void CancelEdit()
+    {
+        var obj = _editingObject;
+        bChanged = false;
+        _editingObject = null;
+        DataGrid.SelectionController.CurrentCellManager.EndEdit(false);
+        if(obj is not null)
+        {
+            UpdateRow(obj.Row, obj.DataRow);
+        }
+    }
+
     private void DataGrid_CurrentCellDropDownSelectionChanged(object? sender,
         CurrentCellDropDownSelectionChangedEventArgs e)
     {
@@ -1646,6 +1697,13 @@ public class DynamicGridGridUIComponent<T> : IDynamicGridUIComponent<T>, IDynami
                 e.Handled = true;
             }
         }
+        else if(e.Key == Key.Escape)
+        {
+            if (Parent.IsDirectEditMode())
+            {
+                bChanged = false;
+            }
+        }
     }
 
     #endregion