Explorar o código

Fixes to row mapping

Kenric Nugteren hai 7 meses
pai
achega
383b13f108

+ 39 - 64
inabox.wpf/DynamicGrid/UIComponent/DynamicGridGridUIComponent.cs

@@ -201,11 +201,8 @@ public class DynamicGridGridUIComponent<T> : IDynamicGridUIComponent<T>, IDynami
         DataGrid.SetValue(ScrollViewer.VerticalScrollBarVisibilityProperty, ScrollBarVisibility.Visible);
         
         DataGrid.FilterChanging += DataGrid_FilterChanging;
-        
         DataGrid.FilterChanged += DataGrid_FilterChanged;
 
-        DataGrid.FilterItemsPopulating += DataGrid_FilterItemsPopulating;
-
         var fltstyle = new Style(typeof(GridFilterControl));
         fltstyle.Setters.Add(new Setter(GridFilterControl.FilterModeProperty, FilterMode.Both));
         fltstyle.Setters.Add(new Setter(GridFilterControl.SortOptionVisibilityProperty, Visibility.Collapsed));
@@ -260,10 +257,9 @@ public class DynamicGridGridUIComponent<T> : IDynamicGridUIComponent<T>, IDynami
         var result = new List<CoreRow>();
         foreach (var item in DataGrid.SelectedItems)
         {
-            if (item is DataRowView datarow)
+            if (item is DataRowView datarow && GetRow(datarow.Row) is CoreRow row)
             {
-                var row = datarow.Row.Table.Rows.IndexOf(datarow.Row);
-                result.Add(Parent.Data.Rows[row]);
+                result.Add(row);
             }
         }
 
@@ -352,8 +348,10 @@ public class DynamicGridGridUIComponent<T> : IDynamicGridUIComponent<T>, IDynami
         var rows = DataGrid.View.Records.Select(x => (x.Data as DataRowView)!).ToList();
         foreach (var row in rows)
         {
-            var iRow = table.Rows.IndexOf(row.Row);
-            result.Add(Parent.Data.Rows[iRow]);
+            if(GetRow(row.Row) is CoreRow coreRow)
+            {
+                result.Add(coreRow);
+            }
         }
 
         return result.ToArray();
@@ -458,10 +456,12 @@ public class DynamicGridGridUIComponent<T> : IDynamicGridUIComponent<T>, IDynami
         if (row is not DataRowView dataRowView || DataGridItems is not DataTable table)
             return null;
 
-        var rowIdx = table.Rows.IndexOf(dataRowView.Row);
-        if (rowIdx < 0)
-            return null;
-        return Parent.Data.Rows[rowIdx];
+        return GetRow(dataRowView.Row);
+    }
+
+    private CoreRow? GetRow(DataRow row)
+    {
+        return _rowMap.GetValueOrDefault(row);
     }
 
     private void DataGrid_CellDoubleTapped(object? sender, GridCellDoubleTappedEventArgs e)
@@ -582,43 +582,6 @@ public class DynamicGridGridUIComponent<T> : IDynamicGridUIComponent<T>, IDynami
         UpdateRecordCount();
     }
 
-    private void DataGrid_FilterItemsPopulating(object? sender, GridFilterItemsPopulatingEventArgs e)
-    {
-        var colIdx = DataGrid.Columns.IndexOf(e.Column);
-        var column = GetColumn(colIdx);
-        if(column is not null)
-        {
-            var filterItems = Parent.GetColumnFilterItems(column);
-            if(filterItems is not null)
-            {
-                e.ItemsSource = filterItems.Select(x =>
-                {
-                    var element = new FilterElement
-                    {
-                        DisplayText = x.Item2,
-                        ActualValue = x.Item1,
-                    };
-                    if(column is DynamicActionColumn dac)
-                    {
-                        element.IsSelected = (dac.SelectedFilters is null || dac.SelectedFilters.Contains(x.Item1))
-                            && (dac.ExcludeFilters is null || !dac.ExcludeFilters.Contains(x.Item1));
-                    }
-                    return element;
-                });
-            }
-            else if (column is DynamicActionColumn dac && dac.Filters is not null)
-            {
-                e.ItemsSource = dac.Filters.Select(x => new FilterElement
-                {
-                    DisplayText = x,
-                    ActualValue = x,
-                    IsSelected = (dac.SelectedFilters is null || dac.SelectedFilters.Contains(x))
-                        && (dac.ExcludeFilters is null || !dac.ExcludeFilters.Contains(x))
-                });
-            }
-        }
-    }
-
     private void UpdateRecordCount()
     {
         var count = DataGrid.View != null ? DataGrid.View.Records.Count : Parent.Data.Rows.Count;
@@ -753,6 +716,13 @@ public class DynamicGridGridUIComponent<T> : IDynamicGridUIComponent<T>, IDynami
                 headStyle.Setters.Add(new Setter(Control.HorizontalContentAlignmentProperty, HorizontalAlignment.Left));
                 headStyle.Setters.Add(new Setter(Control.TemplateProperty,
                     Application.Current.Resources["VerticalColumnHeader"] as ControlTemplate));
+                // headStyle.AddSetter(GridHeaderCellControl.ContentTemplateProperty,
+                //     TemplateGenerator.CreateDataTemplate(() =>
+                //     {
+                //         var content = new ContentPresenter();
+                //         content.SetBinding(ContentPresenter.ContentProperty, new Binding());
+                //         return content;
+                //     }));
             }
         }
         return headStyle;
@@ -994,6 +964,8 @@ public class DynamicGridGridUIComponent<T> : IDynamicGridUIComponent<T>, IDynami
     {
         if (Parent.GetColumnFilter(column) is not IDynamicGridColumnFilter filter) return;
 
+        var vertical = column is DynamicActionColumn ac && ac.VerticalHeader && !ac.HeaderText.IsNullOrWhiteSpace();
+
         var horizontalAlignment = gridColumn.HorizontalHeaderContentAlignment;
         gridColumn.HorizontalHeaderContentAlignment = HorizontalAlignment.Stretch;
         gridColumn.HeaderTemplate = TemplateGenerator.CreateDataTemplate(() =>
@@ -1011,6 +983,12 @@ public class DynamicGridGridUIComponent<T> : IDynamicGridUIComponent<T>, IDynami
             var button = new DynamicGridColumnFilterUIButton(filter);
             grid.AddChild(button, 0, 1);
 
+            if(vertical)
+            {
+                button.LayoutTransform = new RotateTransform(90);
+                content.HorizontalAlignment = HorizontalAlignment.Stretch;
+            }
+
             return grid;
         });
 
@@ -1117,9 +1095,8 @@ public class DynamicGridGridUIComponent<T> : IDynamicGridUIComponent<T>, IDynami
             {
                 Sum = Aggregate(rows.Select(x =>
                 {
-                    var row = x.Row.Table.Rows.IndexOf(x.Row);
-                    return Grid.Parent.Data.Rows[row];
-                }));
+                    return Grid.GetRow(x.Row);
+                }).NotNull());
             }
             else
             {
@@ -1216,13 +1193,9 @@ public class DynamicGridGridUIComponent<T> : IDynamicGridUIComponent<T>, IDynami
                 content.SetBinding(ContentControl.ContentProperty,
                     WPFUtils.CreateMultiBinding(new MultiFuncConverter(x =>
                     {
-                        if(x[0] is DataRowView view && DataGridItems is DataTable table)
+                        if(x[0] is DataRowView view && DataGridItems is DataTable table && GetRow(view.Row) is CoreRow row)
                         {
-                            var rowIdx = table.Rows.IndexOf(view.Row);
-                            if (rowIdx >= 0)
-                            {
-                                return tmplCol.Template(Parent.Data.Rows[rowIdx]);
-                            }
+                            return tmplCol.Template(row);
                         }
                         return null;
                     }))
@@ -1421,6 +1394,7 @@ public class DynamicGridGridUIComponent<T> : IDynamicGridUIComponent<T>, IDynami
     #region Data
 
     private bool _invalidating = false;
+    private Dictionary<DataRow, CoreRow> _rowMap = new();
 
     public void BeforeRefresh()
     {
@@ -1431,6 +1405,7 @@ public class DynamicGridGridUIComponent<T> : IDynamicGridUIComponent<T>, IDynami
     public void RefreshData(CoreTable data)
     {
         var result = new DataTable();
+        _rowMap.Clear();
 
         var defaults = new List<object?>();
         foreach (var column in data.Columns)
@@ -1456,6 +1431,7 @@ public class DynamicGridGridUIComponent<T> : IDynamicGridUIComponent<T>, IDynami
             var newrow = result.NewRow();
             CoreRowToDataRow(newrow, row, defaults);
             result.Rows.Add(newrow);
+            _rowMap[newrow] = row;
         }
 
         result.ColumnChanged += Result_ColumnChanged;
@@ -1482,6 +1458,7 @@ public class DynamicGridGridUIComponent<T> : IDynamicGridUIComponent<T>, IDynami
         if (clearRows)
         {
             table.Rows.Clear();
+            _rowMap.Clear();
         }
 
         var defaults = new List<object?>();
@@ -1496,6 +1473,7 @@ public class DynamicGridGridUIComponent<T> : IDynamicGridUIComponent<T>, IDynami
             var newRow = table.NewRow();
             CoreRowToDataRow(newRow, row, defaults);
             table.Rows.Add(newRow);
+            _rowMap[newRow] = row;
         }
 
         UpdateRecordCount();
@@ -1730,12 +1708,10 @@ public class DynamicGridGridUIComponent<T> : IDynamicGridUIComponent<T>, IDynami
         if (_invalidating) return;
         if (sender is not DataTable table) return;
 
-        var rowIdx = table.Rows.IndexOf(e.Row);
-        if (rowIdx < 0)
+        var row = GetRow(e.Row);
+        if (row is null)
             return;
 
-        var row = Parent.Data.Rows[rowIdx];
-
         var colIdx = table.Columns.IndexOf(e.Column);
         if (colIdx < 0 || colIdx >= Parent.Data.Columns.Count)
             return;
@@ -1925,8 +1901,7 @@ public class DynamicGridGridUIComponent<T> : IDynamicGridUIComponent<T>, IDynami
             {
                 if (x is DataRowView dataRow)
                 {
-                    var row = dataRow.Row.Table.Rows.IndexOf(dataRow.Row);
-                    return Parent.Data.Rows[row];
+                    return GetRow(dataRow.Row);
                 }
                 else
                 {

+ 10 - 41
inabox.wpf/DynamicGrid/UIComponent/DynamicGridTreeUIComponent.cs

@@ -263,7 +263,6 @@ public class DynamicGridTreeUIComponent<T, TKey> : IDynamicGridUIComponent<T>, I
 
         _tree.FilterChanging += _tree_FilterChanging;
         _tree.FilterChanged += _tree_FilterChanged;
-        _tree.FilterItemsPopulating += _tree_FilterItemsPopulating;
         _tree.FilterLevel = FilterLevel.Extended;
         _tree.SelectionForeground = DynamicGridUtils.SelectionForeground;
         _tree.SelectionBackground = DynamicGridUtils.SelectionBackground;
@@ -549,40 +548,6 @@ public class DynamicGridTreeUIComponent<T, TKey> : IDynamicGridUIComponent<T>, I
 
     private readonly Dictionary<string, string> FilterPredicates = new();
 
-    private void _tree_FilterItemsPopulating(object? sender, Syncfusion.UI.Xaml.TreeGrid.Filtering.TreeGridFilterItemsPopulatingEventArgs e)
-    {
-        var colIdx = _tree.Columns.IndexOf(e.Column);
-        var column = GetColumn(colIdx);
-        if(column is not null)
-        {
-            var filterItems = Parent.GetColumnFilterItems(column);
-            if(filterItems is not null)
-            {
-                e.ItemsSource = filterItems.Select(x => new FilterElement
-                {
-                    DisplayText = x.Item2, ActualValue = x.Item1
-                });
-            }
-            else if (column is DynamicActionColumn dac && dac.Filters is not null)
-            {
-                e.ItemsSource = dac.Filters.Select(x => new FilterElement
-                {
-                    DisplayText = x, ActualValue = x,
-                    IsSelected = (dac.SelectedFilters is null || dac.SelectedFilters.Contains(x))
-                        && (dac.ExcludeFilters is null || dac.ExcludeFilters.Contains(x))
-                });
-            }
-            else if (column is DynamicGridColumn dgc)
-            {
-                var preds = e.Column.FilterPredicates.Select(x => x.FilterValue).ToArray();
-                var data = Parent.Data.Rows.Select(r => r.Get<String>(dgc.ColumnName)).OrderBy(x=>x);
-                
-                e.ItemsSource = data.Select(x => new FilterElement()
-                    { DisplayText = x ?? "(Blanks)", ActualValue = x,  IsSelected = preds.Length == 0 || preds.Contains(x) });
-            }
-        }
-    }
-    
     private void _tree_FilterChanging(object? sender, TreeGridFilterChangingEventArgs e)
     {
         var col = GetColumn(_tree.Columns.IndexOf(e.Column)) as DynamicActionColumn;
@@ -1036,10 +1001,7 @@ public class DynamicGridTreeUIComponent<T, TKey> : IDynamicGridUIComponent<T>, I
     {
         if (row is null) return null;
 
-        var index = row.Index;
-        if (index < 0 || index >= Parent.Data.Rows.Count) return null;
-
-        return Parent.Data.Rows[row.Index];
+        return _rowMap.GetValueOrDefault(row);
     }
 
     private CoreTreeNode<TKey>? GetNode(CoreRow row)
@@ -1648,6 +1610,8 @@ public class DynamicGridTreeUIComponent<T, TKey> : IDynamicGridUIComponent<T>, I
 
     public CoreTreeNodes<TKey> Nodes { get; set; }
 
+    private Dictionary<CoreRow, CoreRow> _rowMap = new();
+
     private CoreTable? _innerTable;
     private bool _invalidating = false;
 
@@ -1665,6 +1629,8 @@ public class DynamicGridTreeUIComponent<T, TKey> : IDynamicGridUIComponent<T>, I
     {
         var nodes = new CoreTreeNodes<TKey>(NullKey);
 
+        _rowMap.Clear();
+
         _innerTable = new CoreTable();
         _innerTable.LoadColumns(data.Columns);
 
@@ -1685,6 +1651,7 @@ public class DynamicGridTreeUIComponent<T, TKey> : IDynamicGridUIComponent<T>, I
             var newRow = _innerTable.NewRow();
             ProcessRow(newRow, row);
             _innerTable.Rows.Add(newRow);
+            _rowMap[newRow] = row;
 
             nodes.Add(GetIDKey(row), GetParentKey(row), newRow);
         }
@@ -1713,6 +1680,7 @@ public class DynamicGridTreeUIComponent<T, TKey> : IDynamicGridUIComponent<T>, I
         {
             _innerTable.Rows.Clear();
             Nodes.Clear();
+            _rowMap.Clear();
         }
 
         foreach(var row in rows.Where(FilterRow))
@@ -1720,6 +1688,7 @@ public class DynamicGridTreeUIComponent<T, TKey> : IDynamicGridUIComponent<T>, I
             var newRow = _innerTable.NewRow();
             ProcessRow(newRow, row);
             _innerTable.Rows.Add(newRow);
+            _rowMap[newRow] = row;
 
             Nodes.Add(GetIDKey(row), GetParentKey(row), newRow);
         }
@@ -1749,10 +1718,10 @@ public class DynamicGridTreeUIComponent<T, TKey> : IDynamicGridUIComponent<T>, I
 
     private void CalculateRowHeight()
     {
-        if(Parent.Data != null && Parent.Data.Rows.Count > 0)
+        if(_innerTable is not null && _innerTable.Rows.Count > 0)
         {
             var contentHeight = _tree.ActualHeight - (_tree.Padding.Top + _tree.Padding.Bottom) - 2; // Two extra pixels of space
-            var targetHeight = contentHeight / Parent.Data.Rows.Count;
+            var targetHeight = contentHeight / _innerTable.Rows.Count;
             _tree.RowHeight = Math.Max(Math.Min(targetHeight, MaxRowHeight), MinRowHeight);
         }
     }