Ver código fonte

Added BeforeRefresh and AfterRefresh handlers to Dynamic Grids
Added Cell-level styling hooks to Dynamic Grid

Frank van den Bos 2 anos atrás
pai
commit
68e7cc5f56

+ 27 - 21
inabox.wpf/DynamicGrid/BaseDynamicGrid.cs

@@ -1,12 +1,15 @@
 using System;
 using System.Collections.Generic;
+using System.ComponentModel;
 using System.Linq;
 using System.Linq.Expressions;
 using System.Windows;
 using System.Windows.Controls;
 using System.Windows.Media;
 using System.Windows.Media.Imaging;
+using FastReport.Editor;
 using InABox.Core;
+using Selection = InABox.Core.Selection;
 
 namespace InABox.DynamicGrid
 {
@@ -51,7 +54,14 @@ namespace InABox.DynamicGrid
         public delegate void ValidateEvent(object sender, T[] items, List<string> errors);
         
         public event OnPrintData? OnPrintData;
-        public event OnAfterReloadEventHandler? OnAfterReload;
+        
+        public event BeforeRefreshEventHandler? BeforeRefresh;
+        protected void NotifyBeforeRefresh(BeforeRefreshEventArgs args) => BeforeRefresh?.Invoke(this, args);
+        protected abstract bool OnBeforeRefresh();
+        
+        public event AfterRefreshEventHandler? AfterRefresh;
+        protected void NotifyAfterRefresh(AfterRefreshEventArgs args) => AfterRefresh?.Invoke(this, args);
+        protected abstract void OnAfterRefresh();
         
         public abstract event EntitySaveEvent? OnAfterSave;
         public abstract event EntitySaveEvent? OnBeforeSave;
@@ -63,17 +73,22 @@ namespace InABox.DynamicGrid
         public virtual event OnCustomiseColumns? OnCustomiseColumns;
         public abstract event OnDoubleClick? OnDoubleClick;
 
-        public OnGetDynamicGridStyle? OnGetStyle { get; set; }
-
+        public OnGetDynamicGridRowStyle? OnGetRowStyle { get; set; }
+        
         public ValidateEvent? OnValidate;
-        protected DynamicGridStyleSelector<T> StyleSelector;
+        
+        protected DynamicGridRowStyleSelector<T> RowStyleSelector;
 
+
+        
         public BaseDynamicGrid()
         {
             Options = new FluentList<DynamicGridOption>();
             Options.OnChanged += OptionsChanged;
-            StyleSelector = GetStyleSelector();
-            StyleSelector.GetStyle += (row, style) => GetStyle(row, style);
+            
+            RowStyleSelector = GetRowStyleSelector();
+            RowStyleSelector.GetStyle += (row, style) => GetRowStyle(row, style);
+            
             HiddenColumns = new List<Expression<Func<T, object?>>>();
         }
 
@@ -124,13 +139,14 @@ namespace InABox.DynamicGrid
 
         public abstract void UpdateRow<TType>(CoreRow row, string column, TType value, bool refresh = true);
         public abstract void UpdateRow<T, TType>(CoreRow row, Expression<Func<T, TType>> column, TType value, bool refresh = true);
-        protected abstract DynamicGridStyleSelector<T> GetStyleSelector();
+        
+        protected abstract DynamicGridRowStyleSelector<T> GetRowStyleSelector();
 
-        protected virtual DynamicGridStyle GetStyle(CoreRow row, DynamicGridStyle style)
+        protected virtual DynamicGridStyle GetRowStyle(CoreRow row, DynamicGridStyle style)
         {
-            return OnGetStyle != null ? OnGetStyle(row, style) : style;
+            return OnGetRowStyle != null ? OnGetRowStyle(row, style) : style;
         }
-
+        
         public abstract void InitialiseEditorForm(IDynamicEditorForm editor, T[] items, Func<Type, CoreTable>? pageDataHandler = null, bool preloadPages = false);
         public abstract bool EditItems(T[] items, Func<Type, CoreTable>? PageDataHandler = null, bool PreloadPages = false);
 
@@ -155,17 +171,7 @@ namespace InABox.DynamicGrid
         }
 
         protected abstract void OptionsChanged(object sender, EventArgs args);
-
-        protected virtual void AfterReload()
-        {
-        }
-
-        protected void DoAfterReload()
-        {
-            AfterReload();
-            OnAfterReload?.Invoke(this);
-        }
-
+        
         public IEnumerable<TType> ExtractValues<TType>(Expression<Func<T, TType>> column, Selection selection)
         {
             var result = selection == Selection.None

+ 2 - 2
inabox.wpf/DynamicGrid/DynamicDataGrid.cs

@@ -217,9 +217,9 @@ namespace InABox.DynamicGrid
         }
 
         private CoreRow[]? SelectedBeforeRefresh;
-        protected override void AfterReload()
+        protected override void OnAfterRefresh()
         {
-            base.AfterReload();
+            base.OnAfterRefresh();
 
             if(SelectedBeforeRefresh is not null)
             {

+ 106 - 22
inabox.wpf/DynamicGrid/DynamicGrid.cs

@@ -32,10 +32,12 @@ using Syncfusion.Windows.Controls.Grid;
 using Syncfusion.Windows.Shared;
 using Syncfusion.Windows.Tools.Controls;
 using Syncfusion.XPS;
+using Brush = System.Windows.Media.Brush;
 using Color = System.Drawing.Color;
 using DataColumn = System.Data.DataColumn;
 using DataRow = System.Data.DataRow;
 using FilterElement = Syncfusion.UI.Xaml.Grid.FilterElement;
+using FontStyle = System.Windows.FontStyle;
 using Geometry = System.Windows.Media.Geometry;
 using GridCellToolTipOpeningEventArgs = Syncfusion.UI.Xaml.Grid.GridCellToolTipOpeningEventArgs;
 using GridFilterEventArgs = Syncfusion.UI.Xaml.Grid.GridFilterEventArgs;
@@ -155,13 +157,34 @@ namespace InABox.DynamicGrid
         }
     }
 
-    public class NewDynamicGridStyle : DynamicGridStyle<VirtualizingCellsControl>
+    public class DynamicGridRowStyle : DynamicGridStyle<VirtualizingCellsControl>
     {
-        public NewDynamicGridStyle() : base(null)
+        public DynamicGridRowStyle() : base(null)
         {
         }
 
-        public NewDynamicGridStyle(IDynamicGridStyle source) : base(source)
+        public DynamicGridRowStyle(IDynamicGridStyle source) : base(source)
+        {
+        }
+
+        public override DependencyProperty FontSizeProperty => Control.FontSizeProperty;
+
+        public override DependencyProperty FontStyleProperty => Control.FontStyleProperty;
+
+        public override DependencyProperty FontWeightProperty => Control.FontWeightProperty;
+
+        public override DependencyProperty BackgroundProperty => Control.BackgroundProperty;
+
+        public override DependencyProperty ForegroundProperty => Control.ForegroundProperty;
+    }
+    
+    public class DynamicGridCellStyle : DynamicGridStyle<Control>
+    {
+        public DynamicGridCellStyle() : base(null)
+        {
+        }
+
+        public DynamicGridCellStyle(IDynamicGridStyle source) : base(source)
         {
         }
 
@@ -392,7 +415,19 @@ namespace InABox.DynamicGrid
         public override event EntitySaveEvent? OnAfterSave;
         
         #endregion
-
+        
+        private DynamicGridCellStyleConverter<System.Windows.Media.Brush?> CellBackgroundConverter;
+        private DynamicGridCellStyleConverter<System.Windows.Media.Brush?> CellForegroundConverter;
+        private DynamicGridCellStyleConverter<double?> CellFontSizeConverter;
+        private DynamicGridCellStyleConverter<System.Windows.FontStyle?> CellFontStyleConverter;
+        private DynamicGridCellStyleConverter<System.Windows.FontWeight?> CellFontWeightConverter;
+
+        protected virtual Brush? GetCellBackground(CoreRow row, String columnname) => null;
+        protected virtual Brush? GetCellForeground(CoreRow row, String columnname) => null;
+        protected virtual double? GetCellFontSize(CoreRow row, String columnname) => null;
+        protected virtual FontStyle? GetCellFontStyle(CoreRow row, String columnname) => null;
+        protected virtual FontWeight? GetCellFontWeight(CoreRow row, String columnname) => null;
+        
         public DynamicGrid() : base()
         {
             IsReady = false;
@@ -423,6 +458,12 @@ namespace InABox.DynamicGrid
                 ActionColumns.Add(down);
                 HiddenColumns.Add(x => (x as ISequenceable)!.Sequence);
             }
+            
+            CellBackgroundConverter = new DynamicGridCellStyleConverter<System.Windows.Media.Brush?>(this, GetCellBackground);
+            CellForegroundConverter = new DynamicGridCellStyleConverter<System.Windows.Media.Brush?>(this, GetCellForeground);
+            CellFontSizeConverter = new DynamicGridCellStyleConverter<double?>(this, GetCellFontSize);
+            CellFontStyleConverter = new DynamicGridCellStyleConverter<System.Windows.FontStyle?>(this, GetCellFontStyle);
+            CellFontWeightConverter = new DynamicGridCellStyleConverter<System.Windows.FontWeight?>(this, GetCellFontWeight);
 
             VisibleColumns = new DynamicGridColumns();
 
@@ -477,7 +518,7 @@ namespace InABox.DynamicGrid
             fltstyle.Setters.Add(new Setter(GridFilterControl.SortOptionVisibilityProperty, Visibility.Collapsed));
             DataGrid.FilterPopupStyle = fltstyle;
 
-            DataGrid.RowStyleSelector = StyleSelector;
+            DataGrid.RowStyleSelector = RowStyleSelector;
             DataGrid.TableSummaryCellStyleSelector = new DynamicGridSummaryStyleSelector(this);
 
             //DataGrid.MouseMove += DataGrid_MouseMove;
@@ -728,21 +769,21 @@ namespace InABox.DynamicGrid
                 DuplicateBtn.Visibility = Visibility.Collapsed;
         }
 
-        protected override DynamicGridStyleSelector<T> GetStyleSelector()
+        protected override DynamicGridRowStyleSelector<T> GetRowStyleSelector()
         {
-            return new DynamicGridStyleSelector<T, NewDynamicGridStyle>();
+            return new DynamicGridRowStyleSelector<T, DynamicGridRowStyle>();
         }
-
-        protected override DynamicGridStyle GetStyle(CoreRow row, DynamicGridStyle style)
+        
+        protected override DynamicGridStyle GetRowStyle(CoreRow row, DynamicGridStyle style)
         {
-            var result = base.GetStyle(row, style);
+            var result = base.GetRowStyle(row, style);
             if (ClipBuffer != null)
                 if (ClipBuffer.Item2.Contains(row))
                 {
                     var bgbrush = style.Background as SolidColorBrush;
                     var bgcolor = bgbrush != null ? bgbrush.Color : Colors.Transparent;
 
-                    result = new NewDynamicGridStyle(style);
+                    result = new DynamicGridRowStyle(style);
 
                     result.Background = ClipBuffer.Item1 == ClipAction.Cut
                         ? new SolidColorBrush(bgcolor.MixColors(0.5, Colors.Orchid))
@@ -1543,7 +1584,7 @@ namespace InABox.DynamicGrid
         {
             return !IsSequenced;
         }
-
+        
         private void ReloadColumns()
         {
 
@@ -1563,7 +1604,7 @@ namespace InABox.DynamicGrid
             gridRowResizingOptions.ExcludeColumns = new List<string>();
 
             LoadActionColumns(DynamicActionColumnPosition.Start);
-
+            
             foreach (var column in VisibleColumns)
             {
                 var filtering = true;
@@ -1808,9 +1849,18 @@ namespace InABox.DynamicGrid
                         }
 
                         cellstyle.Setters.Add(new Setter(ForegroundProperty, new SolidColorBrush(Colors.Black)));
-
                         newcol.CellStyle = cellstyle;
                     }
+                    else
+                    {
+                        cellstyle.Setters.Add(new Setter(BackgroundProperty, new Binding() { Path=new PropertyPath("."), Converter = CellBackgroundConverter, ConverterParameter = column.ColumnName}));
+                        cellstyle.Setters.Add(new Setter(ForegroundProperty, new Binding() { Converter = CellForegroundConverter, ConverterParameter = column.ColumnName}));
+                        cellstyle.Setters.Add(new Setter(FontSizeProperty, new Binding() { Converter = CellFontSizeConverter, ConverterParameter = column.ColumnName}));
+                        cellstyle.Setters.Add(new Setter(FontStyleProperty, new Binding() { Converter = CellFontStyleConverter, ConverterParameter = column.ColumnName}));
+                        cellstyle.Setters.Add(new Setter(FontWeightProperty, new Binding() { Converter = CellFontWeightConverter, ConverterParameter = column.ColumnName}));
+                        newcol.CellStyle = cellstyle;
+                    }
+                    
 
                     DataGrid.Columns.Add(newcol);
                     ColumnList.Add(column);
@@ -1901,10 +1951,12 @@ namespace InABox.DynamicGrid
 
         public override void Refresh(bool reloadcolumns, bool reloaddata)
         {
-            //DataGrid.View.Filter = DoFilterRecord;
-
+            
             if (bRefreshing)
                 return;
+
+            if (!DoBeforeRefresh())
+                return;
             
             DataGrid.SelectionForegroundBrush = BaseDynamicGrid.SelectionForeground;
             DataGrid.RowSelectionBrush = BaseDynamicGrid.SelectionBackground;
@@ -1963,7 +2015,9 @@ namespace InABox.DynamicGrid
                                 Dispatcher.Invoke(() =>
                                 {
                                     ProcessData(reloadcolumns, reloaddata);
-                                    DoAfterReload();
+                                    DoAfterRefresh();
+                                    bRefreshing = false;
+                                    IsReady = true;
                                 });
                             }
                         }
@@ -1972,15 +2026,16 @@ namespace InABox.DynamicGrid
                 else
                 {
                     ProcessData(reloadcolumns, reloaddata);
-                    DoAfterReload();
+                    DoAfterRefresh();
+                    bRefreshing = false;
+                    IsReady = true;
                     
                     Loading.BeginAnimation(Label.OpacityProperty, null);
                     Loading.Visibility = Visibility.Collapsed;
                 }
             }
 
-            bRefreshing = false;
-            IsReady = true;
+
 
             if (cursor != null)
             {
@@ -1988,7 +2043,35 @@ namespace InABox.DynamicGrid
                 cursor = null;
             }
         }
+        
+        protected override bool OnBeforeRefresh()
+        {
+            return true;
+        }
+
+        private bool DoBeforeRefresh()
+        {
+            var result = OnBeforeRefresh();
+            if (result)
+            {
+                var args = new BeforeRefreshEventArgs() { Cancel = false };
+                NotifyBeforeRefresh(args);
+                result = args.Cancel == false;
+            }
+
+            return result;
+        }
+        
+        protected override void OnAfterRefresh()
+        {
+        }
 
+        protected void DoAfterRefresh()
+        {
+            OnAfterRefresh();
+            NotifyAfterRefresh(new AfterRefreshEventArgs());
+        }
+        
         public Columns<T> DataColumns()
         {
             var columns = new Columns<T>();
@@ -2140,8 +2223,9 @@ namespace InABox.DynamicGrid
                 result.Rows.Add(newrow);
             }
 
-            if (StyleSelector != null)
-                StyleSelector.Data = Data;
+            if (RowStyleSelector != null)
+                RowStyleSelector.Data = Data;
+            
             //int rowIndex = DataGrid.SelectionController.CurrentCellManager.CurrentRowColumnIndex.RowIndex;
             //int columnIndex = DataGrid.SelectionController.CurrentCellManager.CurrentRowColumnIndex.ColumnIndex;
             //int scrollRowIndex = DataGrid.GetVisualContainer().ScrollRows.LastBodyVisibleLineIndex;

+ 6 - 2
inabox.wpf/DynamicGrid/DynamicGridCommon.cs

@@ -104,6 +104,10 @@ namespace InABox.DynamicGrid
     }
 
     public delegate void OnCellDoubleClick(object sender, DynamicGridCellClickEventArgs args);
-
-    public delegate void OnAfterReloadEventHandler(object sender);
+    
+    public class BeforeRefreshEventArgs : CancelEventArgs { }
+    public delegate void BeforeRefreshEventHandler(object sender, BeforeRefreshEventArgs args);
+    
+    public class AfterRefreshEventArgs : EventArgs { }
+    public delegate void AfterRefreshEventHandler(object sender, AfterRefreshEventArgs args);
 }

+ 58 - 7
inabox.wpf/DynamicGrid/DynamicGridStyle.cs

@@ -3,6 +3,7 @@ using System.Data;
 using System.Linq;
 using System.Windows;
 using System.Windows.Controls;
+using System.Windows.Data;
 using System.Windows.Media;
 using InABox.Clients;
 using InABox.Core;
@@ -11,8 +12,8 @@ using Syncfusion.UI.Xaml.Grid;
 
 namespace InABox.DynamicGrid
 {
-    public delegate DynamicGridStyle OnGetDynamicGridStyle(CoreRow row, DynamicGridStyle defaultstyle);
-
+    public delegate DynamicGridStyle OnGetDynamicGridRowStyle(CoreRow row, DynamicGridStyle defaultstyle);
+    
     public interface IBaseDynamicGridStyle
     {
         double FontSize { get; set; }
@@ -128,7 +129,7 @@ namespace InABox.DynamicGrid
         }
     }
 
-    public abstract class DynamicGridStyleSelector<T> : StyleSelector, IBaseDynamicGridStyle
+    public abstract class DynamicGridRowStyleSelector<T> : StyleSelector, IBaseDynamicGridStyle
     {
         private static bool initialized;
 
@@ -137,8 +138,7 @@ namespace InABox.DynamicGrid
         private static DynamicGridStyle defaultstyle;
 
         public CoreTable Data { get; set; }
-
-
+        
         public double FontSize
         {
             get => defaultstyle.FontSize;
@@ -213,7 +213,7 @@ namespace InABox.DynamicGrid
             initialized = true;
         }
 
-        public event OnGetDynamicGridStyle? GetStyle;
+        public event OnGetDynamicGridRowStyle? GetStyle;
 
         private DynamicGridStyle ExecuteHelper(CoreRow row, DynamicGridStyle style)
         {
@@ -274,11 +274,62 @@ namespace InABox.DynamicGrid
         }
     }
 
-    public class DynamicGridStyleSelector<TEntity, TStyle> : DynamicGridStyleSelector<TEntity> where TStyle : DynamicGridStyle, new()
+    public class DynamicGridRowStyleSelector<TEntity, TStyle> : DynamicGridRowStyleSelector<TEntity> where TStyle : DynamicGridStyle, new()
     {
         protected override DynamicGridStyle CreateStyle()
         {
             return new TStyle();
         }
     }
+
+    public class DynamicGridCellStyleConverter<T> : IValueConverter
+    {
+
+        private IDynamicGrid _grid;
+        
+        private Func<CoreRow, String, T> _converter;
+        
+        public DynamicGridCellStyleConverter(IDynamicGrid grid, Func<CoreRow, String, T> converter)
+        {
+            _grid = grid ?? throw new ArgumentNullException(nameof(grid));
+            _converter = converter ?? throw new ArgumentNullException(nameof(converter));
+        }
+
+        private CoreRow? GetRow(object item)
+        {
+            try
+            {
+                var row = item as DataRowView;
+                if (row != null)
+                {
+                    var index = row.Row.Table.Rows.IndexOf(row.Row);
+                    return _grid.Data.Rows[index];
+                }
+                return null;
+            }
+            catch
+            {
+                return null;
+            }
+        }
+        
+        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
+        {
+            var row = GetRow(value);
+            if (row == null)
+                return DependencyProperty.UnsetValue;
+            
+            var column = parameter as String;
+            if (String.IsNullOrWhiteSpace(column))
+                return DependencyProperty.UnsetValue;
+            
+            return _converter.Invoke(row,column) ?? DependencyProperty.UnsetValue;
+        }
+        
+        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
+        {
+            throw new NotSupportedException();
+        }
+    }
+    
 }

+ 3 - 1
inabox.wpf/DynamicGrid/IDynamicGrid.cs

@@ -55,7 +55,9 @@ namespace InABox.DynamicGrid
         
         event OnCustomiseColumns OnCustomiseColumns;
 
-        event OnAfterReloadEventHandler OnAfterReload;
+        event BeforeRefreshEventHandler BeforeRefresh;
+        
+        event AfterRefreshEventHandler AfterRefresh;
         
         event EntitySaveEvent? OnAfterSave;
         event EntitySaveEvent? OnBeforeSave;

+ 2 - 2
inabox.wpf/DynamicGrid/MasterList.xaml.cs

@@ -69,7 +69,7 @@ namespace InABox.DynamicGrid
 
             grid.OnPrintData += PrintData;
 
-            grid.OnAfterReload += Grid_AfterReload;
+            grid.AfterRefresh += Grid_AfterReload;
 
             grid.OnFilterRecord += Grid_OnFilterRecord;
 
@@ -87,7 +87,7 @@ namespace InABox.DynamicGrid
         public string? SelectedGroup { get; set; }
         public List<Tuple<string, object?, BitmapImage>> GroupList { get; private set; }
 
-        private void Grid_AfterReload(object sender)
+        private void Grid_AfterReload(object sender, AfterRefreshEventArgs args)
         {
             if (bLoaded)
                 return;