Kenric Nugteren 1 год назад
Родитель
Сommit
6424ce2a23
1 измененных файлов с 98 добавлено и 10 удалено
  1. 98 10
      inabox.wpf/DynamicGrid/UIComponent/DynamicGridGridUIComponent.cs

+ 98 - 10
inabox.wpf/DynamicGrid/UIComponent/DynamicGridGridUIComponent.cs

@@ -3,6 +3,7 @@ using InABox.Core;
 using InABox.WPF;
 using org.omg.PortableInterceptor;
 using Syncfusion.Data;
+using Syncfusion.Data.Extensions;
 using Syncfusion.UI.Xaml.Grid;
 using Syncfusion.UI.Xaml.Grid.Cells;
 using Syncfusion.UI.Xaml.Grid.Helpers;
@@ -106,7 +107,6 @@ public class DynamicGridGridUIComponent<T> : IDynamicGridUIComponent<T>, IDynami
 
         DataGrid.CurrentCellBeginEdit += DataGrid_CurrentCellBeginEdit;
         DataGrid.CurrentCellEndEdit += DataGrid_CurrentCellEndEdit;
-        DataGrid.CurrentCellValueChanged += DataGrid_CurrentCellValueChanged;
         DataGrid.CurrentCellDropDownSelectionChanged += DataGrid_CurrentCellDropDownSelectionChanged;
         DataGrid.PreviewKeyUp += DataGrid_PreviewKeyUp;
 
@@ -166,6 +166,63 @@ public class DynamicGridGridUIComponent<T> : IDynamicGridUIComponent<T>, IDynami
         //DataGrid.HeaderStyle = headstyle;
 
         DataGrid.SizeChanged += DataGrid_SizeChanged;
+
+        //DataGrid.CellRenderers.Remove("Numeric");
+        //DataGrid.CellRenderers.Add("Numeric", new CustomNumericCellRenderer(this));
+        //DataGrid.CellRenderers.Remove("TextBox");
+        //DataGrid.CellRenderers.Add("TextBox", new CustomTextCellRenderer(this));
+    }
+
+    //private class CustomTextCellRenderer : GridCellTextBoxRenderer
+    //{
+    //    private DynamicGridGridUIComponent<T> Component;
+
+    //    public CustomTextCellRenderer(DynamicGridGridUIComponent<T> component)
+    //    {
+    //        Component = component;
+    //    }
+
+    //    public override void OnInitializeEditElement(DataColumnBase dataColumn, TextBox uiElement, object dataContext)
+    //    {
+    //        base.OnInitializeEditElement(dataColumn, uiElement, dataContext);
+    //        uiElement.TextChanged += UiElement_TextChanged;
+    //    }
+
+    //    private void UiElement_TextChanged(object sender, TextChangedEventArgs e)
+    //    {
+    //        throw new NotImplementedException();
+    //    }
+
+    //    private void UiElement_ValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
+    //    {
+    //        Component.ChangeValue(e.OldValue, e.NewValue);
+    //    }
+    //}
+
+    //private class CustomNumericCellRenderer : GridCellNumericRenderer
+    //{
+    //    private DynamicGridGridUIComponent<T> Component;
+
+    //    public CustomNumericCellRenderer(DynamicGridGridUIComponent<T> component)
+    //    {
+    //        Component = component;
+    //    }
+
+    //    public override void OnInitializeEditElement(DataColumnBase dataColumn, DoubleTextBox uiElement, object dataContext)
+    //    {
+    //        base.OnInitializeEditElement(dataColumn, uiElement, dataContext);
+    //        uiElement.ValueChanged += UiElement_ValueChanged;
+    //    }
+
+    //    private void UiElement_ValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
+    //    {
+    //        Component.ChangeValue(e.OldValue, e.NewValue);
+    //    }
+    //}
+
+    private void ChangeValue(object oldValue, object newValue)
+    {
+
     }
 
     private void ColumnsMenu_ContextMenuOpening(object sender, RoutedEventArgs e)
@@ -1091,6 +1148,8 @@ public class DynamicGridGridUIComponent<T> : IDynamicGridUIComponent<T>, IDynami
 
     #region Data
 
+    private bool _invalidating = false;
+
     public void BeforeRefresh()
     {
         DataGrid.SelectionForegroundBrush = GetCellSelectionForegroundBrush();
@@ -1127,6 +1186,8 @@ public class DynamicGridGridUIComponent<T> : IDynamicGridUIComponent<T>, IDynami
             result.Rows.Add(newrow);
         }
 
+        result.ColumnChanged += Result_ColumnChanged;
+
         //int rowIndex = DataGrid.SelectionController.CurrentCellManager.CurrentRowColumnIndex.RowIndex;
         //int columnIndex = DataGrid.SelectionController.CurrentCellManager.CurrentRowColumnIndex.ColumnIndex;
         //int scrollRowIndex = DataGrid.GetVisualContainer().ScrollRows.LastBodyVisibleLineIndex;
@@ -1145,6 +1206,8 @@ public class DynamicGridGridUIComponent<T> : IDynamicGridUIComponent<T>, IDynami
             return;
         }
 
+        _invalidating = true;
+
         var rowdata = new List<object?>(row.Values);
         foreach (var ac in ActionColumns)
             rowdata.Add(ac.Data(row));
@@ -1152,6 +1215,7 @@ public class DynamicGridGridUIComponent<T> : IDynamicGridUIComponent<T>, IDynami
         var datarow = DataGridItems.Rows[row.Index];
         for (var i = 0; i < rowdata.Count; i++)
             datarow[i] = rowdata[i] ?? DBNull.Value;
+        _invalidating = false;
         //datarow.ItemArray = rowdata.ToArray(); 
     }
 
@@ -1322,22 +1386,46 @@ public class DynamicGridGridUIComponent<T> : IDynamicGridUIComponent<T>, IDynami
         bChanged = false;
     }
 
-    private void DataGrid_CurrentCellValueChanged(object? sender, CurrentCellValueChangedEventArgs e)
+    private void Result_ColumnChanged(object sender, DataColumnChangeEventArgs e)
     {
-        var row = GetRowFromIndex(e.RowColumnIndex.RowIndex);
-        // Are we sure that this function is ever useful? It seems that since the data in the grid hasn't been updated by this point, this function is essentially useless (the data is updated in EndEdit). Probably need to check the GridCheckBoxColumn
+        if (_invalidating) return;
+        if (sender is not DataTable table) return;
+
+        var rowIdx = table.Rows.IndexOf(e.Row);
+        if (rowIdx < 0)
+            return;
+
+        var row = Parent.Data.Rows[rowIdx];
 
-        if (row is null) return;
+        var colIdx = table.Columns.IndexOf(e.Column);
+        if (colIdx < 0 || colIdx >= Parent.Data.Columns.Count)
+            return;
+
+        var data = Parent.Data;
 
-        if (e.Column is GridCheckBoxColumn)
+        var dataCol = Parent.Data.Columns[colIdx];
+        var col = ColumnList.OfType<DynamicGridColumn>()
+            .FirstOrDefault(x => x.ColumnName.Equals(dataCol.ColumnName));
+        
+        if (col is null)
+            return;
+
+        if (col is DynamicGridCheckBoxColumn<T>)
         {
             EnsureEditingObject(row);
-        }
+            if(_editingObject is not null)
+            {
+                var value = e.Row[e.Column!];
+                if (value is DBNull)
+                    value = CoreUtils.GetDefault(dataCol.DataType);
+
+                _invalidating = true;
+                UpdateData(dataCol.ColumnName, new Dictionary<CoreColumn, object?>() { { dataCol, value } });
+                _invalidating = false;
+            }
 
-        if (_editingObject is not null)
-            UpdateData(_editingObject.Row.Index, e.RowColumnIndex.ColumnIndex);
-        if (e.Column is GridCheckBoxColumn)
             _editingObject = null;
+        }
         if (_editingObject is not null)
             bChanged = true;
     }