Bläddra i källkod

Fix to InvalidateRow not updating ActionColumns and cell styles

Kenric Nugteren 10 månader sedan
förälder
incheckning
bb093b882e

+ 44 - 0
inabox.wpf/Converters/MultiConverters/MultiFuncConverter.cs

@@ -0,0 +1,44 @@
+using System;
+using System.Collections.Generic;
+using System.Globalization;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows.Data;
+
+namespace InABox.Wpf;
+
+public class MultiFuncConverter(Func<object?[], object?> convert, Func<object?, object?[]?>? convertBack = null, IValueConverter? wrappedConverter = null) : IMultiValueConverter
+{
+    public Func<object?[], object?> ConvertFunc { get; set; } = convert;
+
+    public Func<object?, object?[]?>? ConvertBackFunc { get; set; } = convertBack;
+
+    public IValueConverter? wrappedConverter = wrappedConverter;
+
+    public object? Convert(object?[] values, Type targetType, object parameter, CultureInfo culture)
+    {
+        var val = ConvertFunc(values);
+        if(wrappedConverter is not null)
+        {
+            return wrappedConverter.Convert(val, targetType, parameter, culture);
+        }
+        else
+        {
+            return val;
+        }
+    }
+
+    public object?[]? ConvertBack(object? value, Type[] targetTypes, object parameter, CultureInfo culture)
+    {
+        if(wrappedConverter is not null)
+        {
+            value = wrappedConverter.Convert(value, typeof(object), parameter, culture);
+        }
+        if(ConvertBackFunc is not null)
+        {
+            return ConvertBackFunc(value);
+        }
+        return null;
+    }
+}

+ 67 - 132
inabox.wpf/DynamicGrid/UIComponent/DynamicGridGridUIComponent.cs

@@ -1,5 +1,7 @@
 using InABox.Clients;
 using InABox.Core;
+using InABox.Scripting;
+using InABox.Wpf;
 using InABox.WPF;
 using org.omg.PortableInterceptor;
 using Syncfusion.Data;
@@ -883,6 +885,40 @@ public class DynamicGridGridUIComponent<T> : IDynamicGridUIComponent<T>, IDynami
 
     private ObservableCollection<ISummaryColumn> Summaries = new();
 
+    private void AddCellStyleConverters(Style cellstyle, DynamicColumnBase column, string sColName)
+    {
+        cellstyle.Setters.Add(new Setter(Control.BackgroundProperty,
+            WPFUtils.CreateMultiBinding(
+                CellBackgroundConverter.WrapConverter(x => x[0]),
+                parameter: new DynamicGridCellStyleParameters(column, DependencyProperty.UnsetValue))
+                .AddBinding(new Binding("."))
+                .AddBinding(new Binding(sColName))));
+        cellstyle.Setters.Add(new Setter(Control.ForegroundProperty,
+            WPFUtils.CreateMultiBinding(
+                CellForegroundConverter.WrapConverter(x => x[0]),
+                parameter: new DynamicGridCellStyleParameters(column, DependencyProperty.UnsetValue))
+                .AddBinding(new Binding("."))
+                .AddBinding(new Binding(sColName))));
+        cellstyle.Setters.Add(new Setter(Control.FontSizeProperty,
+            WPFUtils.CreateMultiBinding(
+                CellFontSizeConverter.WrapConverter(x => x[0]),
+                parameter: new DynamicGridCellStyleParameters(column, DependencyProperty.UnsetValue))
+                .AddBinding(new Binding("."))
+                .AddBinding(new Binding(sColName))));
+        cellstyle.Setters.Add(new Setter(Control.FontStyleProperty,
+            WPFUtils.CreateMultiBinding(
+                CellFontStyleConverter.WrapConverter(x => x[0]),
+                parameter: new DynamicGridCellStyleParameters(column, DependencyProperty.UnsetValue))
+                .AddBinding(new Binding("."))
+                .AddBinding(new Binding(sColName))));
+        cellstyle.Setters.Add(new Setter(Control.FontWeightProperty,
+            WPFUtils.CreateMultiBinding(
+                CellFontWeightConverter.WrapConverter(x => x[0]),
+                parameter: new DynamicGridCellStyleParameters(column, DependencyProperty.UnsetValue))
+                .AddBinding(new Binding("."))
+                .AddBinding(new Binding(sColName))));
+    }
+
     private void LoadActionColumns(DynamicActionColumnPosition position)
     {
         for (var i = 0; i < ActionColumns.Count; i++)
@@ -968,38 +1004,7 @@ public class DynamicGridGridUIComponent<T> : IDynamicGridUIComponent<T>, IDynami
                     newcol.HeaderStyle = GetHeaderCellStyle(column);
                     
                     var cellstyle = new Style();
-                    cellstyle.Setters.Add(new Setter(Control.BackgroundProperty,
-                        new Binding()
-                        {
-                            Path = new PropertyPath("."), Converter = CellBackgroundConverter,
-                            ConverterParameter = new DynamicGridCellStyleParameters(column,DependencyProperty.UnsetValue)
-                        }));
-                    cellstyle.Setters.Add(new Setter(Control.ForegroundProperty,
-                        new Binding()
-                        {
-                            Converter = CellForegroundConverter, 
-                            ConverterParameter = new DynamicGridCellStyleParameters(column,DependencyProperty.UnsetValue)
-                        }));
-                    cellstyle.Setters.Add(new Setter(Control.FontSizeProperty,
-                        new Binding()
-                        { 
-                            Converter = CellFontSizeConverter, 
-                            ConverterParameter = new DynamicGridCellStyleParameters(column,DependencyProperty.UnsetValue)
-                            
-                        }));
-                    cellstyle.Setters.Add(new Setter(Control.FontStyleProperty,
-                        new Binding()
-                        { 
-                            Converter = CellFontStyleConverter, 
-                            ConverterParameter = new DynamicGridCellStyleParameters(column,DependencyProperty.UnsetValue)
-                            
-                        }));
-                    cellstyle.Setters.Add(new Setter(Control.FontWeightProperty,
-                        new Binding()
-                        {
-                            Converter = CellFontWeightConverter, 
-                            ConverterParameter = new DynamicGridCellStyleParameters(column,DependencyProperty.UnsetValue)
-                        }));
+                    AddCellStyleConverters(cellstyle, column, sColName);
                     newcol.CellStyle = cellstyle;
 
                     DataGrid.Columns.Add(newcol);
@@ -1008,7 +1013,27 @@ public class DynamicGridGridUIComponent<T> : IDynamicGridUIComponent<T>, IDynami
                 else if (column is DynamicTemplateColumn tmplCol)
                 {
                     var newcol = new GridTemplateColumn();
-                    newcol.CellTemplateSelector = new TemplateColumnSelector(this, tmplCol.Template);
+                    newcol.CellTemplate = TemplateGenerator.CreateDataTemplate(() =>
+                    {
+                        var content = new ContentControl();
+                        content.SetBinding(ContentControl.ContentProperty,
+                            WPFUtils.CreateMultiBinding(new MultiFuncConverter(x =>
+                            {
+                                if(x[0] is DataRowView view && DataGridItems is DataTable table)
+                                {
+                                    var rowIdx = table.Rows.IndexOf(view.Row);
+                                    if (rowIdx >= 0)
+                                    {
+                                        return tmplCol.Template(Parent.Data.Rows[rowIdx]);
+                                    }
+                                }
+                                return null;
+                            }))
+                                .AddBinding(new Binding("."))
+                                .AddBinding(new Binding(sColName)));
+                        return content;
+                    });
+
                     newcol.AllowEditing = false;
                     newcol.UpdateTrigger = UpdateSourceTrigger.PropertyChanged;
                     newcol.MappingName = sColName;
@@ -1031,37 +1056,7 @@ public class DynamicGridGridUIComponent<T> : IDynamicGridUIComponent<T>, IDynami
                     newcol.HeaderStyle = GetHeaderCellStyle(column);
 
                     var cellstyle = new Style();
-                    cellstyle.Setters.Add(new Setter(Control.BackgroundProperty,
-                        new Binding()
-                        {
-                            Path = new PropertyPath("."),
-                            Converter = CellBackgroundConverter,
-                            ConverterParameter = new DynamicGridCellStyleParameters(column,DependencyProperty.UnsetValue)
-                        }));
-                    cellstyle.Setters.Add(new Setter(Control.ForegroundProperty,
-                        new Binding()
-                        {
-                            Converter = CellForegroundConverter,
-                            ConverterParameter = new DynamicGridCellStyleParameters(column,DependencyProperty.UnsetValue)
-                        }));
-                    cellstyle.Setters.Add(new Setter(Control.FontSizeProperty,
-                        new Binding()
-                        {
-                            Converter = CellFontSizeConverter,
-                            ConverterParameter = new DynamicGridCellStyleParameters(column,DependencyProperty.UnsetValue)
-                        }));
-                    cellstyle.Setters.Add(new Setter(Control.FontStyleProperty,
-                        new Binding()
-                        {
-                            Converter = CellFontStyleConverter,
-                            ConverterParameter = new DynamicGridCellStyleParameters(column,DependencyProperty.UnsetValue)
-                        }));
-                    cellstyle.Setters.Add(new Setter(Control.FontWeightProperty,
-                        new Binding()
-                        {
-                            Converter = CellFontWeightConverter,
-                            ConverterParameter = new DynamicGridCellStyleParameters(column,DependencyProperty.UnsetValue)
-                        }));
+                    AddCellStyleConverters(cellstyle, column, sColName);
                     newcol.CellStyle = cellstyle;
 
                     DataGrid.Columns.Add(newcol);
@@ -1098,43 +1093,6 @@ public class DynamicGridGridUIComponent<T> : IDynamicGridUIComponent<T>, IDynami
         }
     }
 
-    public class TemplateColumnSelector(DynamicGridGridUIComponent<T> parent, Func<CoreRow, FrameworkElement?> dataTemplate) : DataTemplateSelector
-    {
-        public Func<CoreRow, FrameworkElement?> DataTemplate { get; init; } = dataTemplate;
-
-        public DynamicGridGridUIComponent<T> Parent { get; init; } = parent;
-
-        public override DataTemplate? SelectTemplate(object item, DependencyObject container)
-        {
-            if (item is not DataRowView) return null;
-
-            CoreRow? row;
-            if(item is DataRowView view && Parent.DataGridItems is DataTable table)
-            {
-                var rowIdx = table.Rows.IndexOf(view.Row);
-                if (rowIdx < 0)
-                {
-                    row = null;
-                }
-                else
-                {
-                    row = Parent.Parent.Data.Rows[rowIdx];
-                }
-            }
-            else
-            {
-                row = null;
-            }
-
-            if (row is null) return null;
-
-            return TemplateGenerator.CreateDataTemplate(() =>
-            {
-                return DataTemplate(row);
-            });
-        }
-    }
-
     private void ApplyFilterStyle(GridColumn column, bool filtering, bool isactioncolumn)
     {
         var filterstyle = new Style();
@@ -1215,36 +1173,7 @@ public class DynamicGridGridUIComponent<T> : IDynamicGridUIComponent<T>, IDynami
                 }
                 else
                 {
-                    cellstyle.Setters.Add(new Setter(Control.BackgroundProperty,
-                        new Binding()
-                        {
-                            Path = new PropertyPath("."), Converter = CellBackgroundConverter,
-                            ConverterParameter = new DynamicGridCellStyleParameters(column,DependencyProperty.UnsetValue)
-                        }));
-                    cellstyle.Setters.Add(new Setter(Control.ForegroundProperty,
-                        new Binding()
-                        {
-                            Converter = CellForegroundConverter, 
-                            ConverterParameter = new DynamicGridCellStyleParameters(column,DependencyProperty.UnsetValue)
-                        }));
-                    cellstyle.Setters.Add(new Setter(Control.FontSizeProperty,
-                        new Binding()
-                        { 
-                            Converter = CellFontSizeConverter, 
-                            ConverterParameter = new DynamicGridCellStyleParameters(column,DependencyProperty.UnsetValue) 
-                        }));
-                    cellstyle.Setters.Add(new Setter(Control.FontStyleProperty,
-                        new Binding()
-                        {
-                            Converter = CellFontStyleConverter, 
-                            ConverterParameter = new DynamicGridCellStyleParameters(column,DependencyProperty.UnsetValue)
-                        }));
-                    cellstyle.Setters.Add(new Setter(Control.FontWeightProperty,
-                        new Binding()
-                        {
-                            Converter = CellFontWeightConverter, 
-                            ConverterParameter = new DynamicGridCellStyleParameters(column,DependencyProperty.UnsetValue)
-                        }));
+                    AddCellStyleConverters(cellstyle, column, newColumn.MappingName);
                     newColumn.CellStyle = cellstyle;
                 }
                 
@@ -1416,11 +1345,17 @@ public class DynamicGridGridUIComponent<T> : IDynamicGridUIComponent<T>, IDynami
         foreach (var ac in ActionColumns)
             rowdata.Add(ac.Data(row));
 
+        //DataGridItems.Rows.RemoveAt(row.Index);
+
+        //var datarow = DataGridItems.NewRow();
         var datarow = DataGridItems.Rows[row.Index];
+
         for (var i = 0; i < rowdata.Count; i++)
             datarow[i] = rowdata[i] ?? DBNull.Value;
+
+        //DataGridItems.Rows.InsertAt(datarow, row.Index);
+
         _invalidating = false;
-        //datarow.ItemArray = rowdata.ToArray(); 
     }
 
     private void CoreRowToDataRow(DataRow newrow, CoreRow row, List<object?> defaults)

+ 28 - 0
inabox.wpf/WPFUtils.cs

@@ -7,6 +7,7 @@ using System.Windows.Controls;
 using System.Windows.Data;
 using System.Windows.Media;
 using InABox.Core;
+using InABox.Wpf;
 using Image = System.Windows.Controls.Image;
 
 namespace InABox.WPF;
@@ -59,6 +60,25 @@ public static class WPFUtils
         return new SolidColorBrush(color) { Opacity = opacity };
     }
 
+    #region Multi-Binding
+
+    public static MultiBinding CreateMultiBinding(IMultiValueConverter? converter = null, string? format = null, object? parameter = null)
+    {
+        return new MultiBinding
+        {
+            Converter = converter,
+            ConverterParameter = parameter,
+            StringFormat = format
+        };
+    }
+    public static MultiBinding AddBinding(this MultiBinding multi, BindingBase binding)
+    {
+        multi.Bindings.Add(binding);
+        return multi;
+    }
+
+    #endregion
+
     #region Binding
 
     public static Binding CreateBinding<T, TProperty>(
@@ -170,6 +190,14 @@ public static class WPFUtils
 
     #endregion
 
+    #region Converters
+
+    public static IMultiValueConverter WrapConverter(this IValueConverter converter, Func<object?[], object?> convert, Func<object?, object?[]?>? convertBack = null)
+    {
+        return new MultiFuncConverter(convert, convertBack, converter);
+    }
+
+    #endregion
 
     public static T? FindLogicalParent<T>(this DependencyObject dependencyObject)
         where T : DependencyObject