فهرست منبع

Added styling for Grouped column headers in DynamicGridGridUIComponent.

Kenric Nugteren 7 ماه پیش
والد
کامیت
867de54493
2فایلهای تغییر یافته به همراه41 افزوده شده و 16 حذف شده
  1. 5 3
      inabox.wpf/DynamicGrid/DynamicGridColumns.cs
  2. 36 13
      inabox.wpf/DynamicGrid/UIComponent/DynamicGridGridUIComponent.cs

+ 5 - 3
inabox.wpf/DynamicGrid/DynamicGridColumns.cs

@@ -88,22 +88,24 @@ public class DynamicGridColumns<T> : DynamicGridColumns
     }
     }
 }
 }
 
 
-public class DynamicGridColumnGroup(string header, DynamicColumnBase start, DynamicColumnBase end)
+public class DynamicGridColumnGroup(string header, DynamicColumnBase start, DynamicColumnBase end, object? tag = null)
 {
 {
     public string Header { get; set; } = header;
     public string Header { get; set; } = header;
 
 
     public DynamicColumnBase StartColumn { get; set; } = start;
     public DynamicColumnBase StartColumn { get; set; } = start;
 
 
     public DynamicColumnBase EndColumn { get; set; } = end;
     public DynamicColumnBase EndColumn { get; set; } = end;
+
+    public object? Tag { get; set; } = tag;
 }
 }
 
 
 public class DynamicGridColumnGrouping
 public class DynamicGridColumnGrouping
 {
 {
     public List<DynamicGridColumnGroup> Groups = new();
     public List<DynamicGridColumnGroup> Groups = new();
 
 
-    public DynamicGridColumnGrouping AddGroup(string header, DynamicColumnBase start, DynamicColumnBase end)
+    public DynamicGridColumnGrouping AddGroup(string header, DynamicColumnBase start, DynamicColumnBase end, object? tag = null)
     {
     {
-        Groups.Add(new(header, start, end));
+        Groups.Add(new(header, start, end, tag: tag));
         return this;
         return this;
     }
     }
 }
 }

+ 36 - 13
inabox.wpf/DynamicGrid/UIComponent/DynamicGridGridUIComponent.cs

@@ -733,6 +733,8 @@ public class DynamicGridGridUIComponent<T> : IDynamicGridUIComponent<T>, IDynami
         DataGrid.ScrollInView(new RowColumnIndex(rowIdx, 0));
         DataGrid.ScrollInView(new RowColumnIndex(rowIdx, 0));
     }
     }
 
 
+    #region Styles
+
     protected virtual Brush? GetCellSelectionForegroundBrush() => DynamicGridUtils.SelectionForeground;
     protected virtual Brush? GetCellSelectionForegroundBrush() => DynamicGridUtils.SelectionForeground;
     protected virtual Brush? GetCellSelectionBackgroundBrush() => DynamicGridUtils.SelectionBackground;
     protected virtual Brush? GetCellSelectionBackgroundBrush() => DynamicGridUtils.SelectionBackground;
     protected virtual Brush? GetCellBackground(CoreRow row, DynamicColumnBase column) => null;
     protected virtual Brush? GetCellBackground(CoreRow row, DynamicColumnBase column) => null;
@@ -781,6 +783,20 @@ public class DynamicGridGridUIComponent<T> : IDynamicGridUIComponent<T>, IDynami
         }
         }
         return headStyle;
         return headStyle;
     }
     }
+
+    protected virtual Style GetColumnGroupHeaderCellStyle(DynamicGridColumnGroup group)
+    {
+        var headstyle = new Style(typeof(GridStackedHeaderCellControl));
+
+        headstyle.Setters.Add(new Setter(Control.BackgroundProperty, new SolidColorBrush(Colors.Gainsboro)));
+        headstyle.Setters.Add(new Setter(Control.ForegroundProperty, new SolidColorBrush(Colors.Black)));
+        headstyle.Setters.Add(new Setter(Control.FontSizeProperty, 12D));
+        headstyle.Setters.Add(new Setter(Control.BorderThicknessProperty, new Thickness(0.0, 0.0, 0, 0)));
+        headstyle.Setters.Add(new Setter(Control.MarginProperty, new Thickness(0, 0, 1, 1)));
+
+        return headstyle;
+    }
+
     protected virtual Style GetSummaryRowStyle()
     protected virtual Style GetSummaryRowStyle()
     {
     {
         var style = new Style(typeof(TableSummaryRowControl));
         var style = new Style(typeof(TableSummaryRowControl));
@@ -817,34 +833,36 @@ public class DynamicGridGridUIComponent<T> : IDynamicGridUIComponent<T>, IDynami
         return style;
         return style;
     }
     }
 
 
+    #endregion
+
     #region Columns
     #region Columns
 
 
     private class StackedHeaderRenderer : GridStackedHeaderCellRenderer
     private class StackedHeaderRenderer : GridStackedHeaderCellRenderer
     {
     {
-        private Style Style;
+        private DynamicGridGridUIComponent<T> Grid;
 
 
-        public StackedHeaderRenderer()
+        public StackedHeaderRenderer(DynamicGridGridUIComponent<T> grid)
         {
         {
-            var headstyle = new Style(typeof(GridStackedHeaderCellControl));
-            headstyle.Setters.Add(new Setter(Control.BackgroundProperty, new SolidColorBrush(Colors.Gainsboro)));
-            headstyle.Setters.Add(new Setter(Control.ForegroundProperty, new SolidColorBrush(Colors.Black)));
-            headstyle.Setters.Add(new Setter(Control.FontSizeProperty, 12D));
-            headstyle.Setters.Add(new Setter(Control.BorderThicknessProperty, new Thickness(0.0, 0.0, 0, 0)));
-            headstyle.Setters.Add(new Setter(Control.MarginProperty, new Thickness(0, 0, 1, 1)));
-
-            Style = headstyle;
+            Grid = grid;
         }
         }
 
 
         public override void OnInitializeEditElement(DataColumnBase dataColumn, GridStackedHeaderCellControl uiElement, object dataContext)
         public override void OnInitializeEditElement(DataColumnBase dataColumn, GridStackedHeaderCellControl uiElement, object dataContext)
         {
         {
-            uiElement.Style = Style;
+            if(dataContext is StackedColumn column && Grid.ColumnGroupMap.TryGetValue(column.MappingName, out var group))
+            {
+                uiElement.Style = Grid.GetColumnGroupHeaderCellStyle(group);
+            }
             base.OnInitializeEditElement(dataColumn, uiElement, dataContext);
             base.OnInitializeEditElement(dataColumn, uiElement, dataContext);
         }
         }
     }
     }
 
 
+    private Dictionary<string, DynamicGridColumnGroup> ColumnGroupMap = new();
+
     private void LoadStackedHeaders(DynamicGridColumnGroupings groupings)
     private void LoadStackedHeaders(DynamicGridColumnGroupings groupings)
     {
     {
         DataGrid.StackedHeaderRows.Clear();
         DataGrid.StackedHeaderRows.Clear();
+        ColumnGroupMap.Clear();
+        var j = 0;
         foreach(var grouping in groupings)
         foreach(var grouping in groupings)
         {
         {
             var row = new StackedHeaderRow();
             var row = new StackedHeaderRow();
@@ -862,11 +880,16 @@ public class DynamicGridGridUIComponent<T> : IDynamicGridUIComponent<T>, IDynami
 
 
                 var cols = Enumerable.Range(start, end - start + 1).Select(i => DataGrid.Columns[i]).ToArray();
                 var cols = Enumerable.Range(start, end - start + 1).Select(i => DataGrid.Columns[i]).ToArray();
 
 
+                var name = $"_GroupColumn{j}";
+
                 var stackedColumn = new StackedColumn
                 var stackedColumn = new StackedColumn
                 {
                 {
                     HeaderText = group.Header,
                     HeaderText = group.Header,
-                    ChildColumns = string.Join(',', cols.Select(x => x.MappingName))
+                    ChildColumns = string.Join(',', cols.Select(x => x.MappingName)),
+                    MappingName = name
                 };
                 };
+                ColumnGroupMap.Add(name, group);
+                j = j + 1;
 
 
                 row.StackedColumns.Add(stackedColumn);
                 row.StackedColumns.Add(stackedColumn);
 
 
@@ -879,7 +902,7 @@ public class DynamicGridGridUIComponent<T> : IDynamicGridUIComponent<T>, IDynami
         if(groupings.Count > 0)
         if(groupings.Count > 0)
         {
         {
             DataGrid.CellRenderers.Remove("StackedHeader");
             DataGrid.CellRenderers.Remove("StackedHeader");
-            DataGrid.CellRenderers.Add("StackedHeader", new StackedHeaderRenderer());
+            DataGrid.CellRenderers.Add("StackedHeader", new StackedHeaderRenderer(this));
         }
         }
     }
     }