Ver Fonte

Fixed a problem with DbFactory Store Selection
Improved Code Popup Editor Behaviour
Refactored MultiSelectDialog and improved "CanAdd" behaviour
Added "ItemsSource" Dependency property to DynamicGrid and DynamicItemsListGrid
Added Parent.CustomiseEditor() to DynamicGridGridUIComponent.LoadDataColumn()

frankvandenbos há 5 meses atrás
pai
commit
9f6d977ab2

+ 4 - 4
InABox.Database/DbFactory.cs

@@ -353,10 +353,10 @@ public static class DbFactory
 
     public static IStore FindStore(Type type, Guid userguid, string userid, Platform platform, string version, Logger logger)
     {
-        var defType = DefaultStore.MakeGenericType(type);
-        Type? subType = Stores.Where(myType => myType.IsSubclassOf(defType)).FirstOrDefault();
-
-        var store = (Activator.CreateInstance(subType ?? defType) as IStore)!;
+        var anyType = typeof(Store<>).MakeGenericType(type);
+        Type? subType = Stores.Where(myType => myType.IsSubclassOf(anyType)).FirstOrDefault();
+        
+        var store = (Activator.CreateInstance(subType ?? DefaultStore.MakeGenericType(type)) as IStore)!;
         
         store.Provider = ProviderFactory.NewProvider(logger);
 

+ 12 - 2
inabox.wpf/DynamicGrid/Columns/EditorColumns/DynamicGridCodePopupColumn.cs

@@ -158,12 +158,15 @@ public class DynamicGridCodePopupColumn<TEntity> : DynamicGridEditorColumn<TEnti
         if (entity != null)
         {
             var msdtype = typeof(MultiSelectDialog<>).MakeGenericType(type);
+            var coltype = typeof(Columns<>).MakeGenericType(type);
+            var cols = (Activator.CreateInstance(coltype, ColumnTypeFlags.IncludeID) as IColumns);
+            
             var msd = Activator.CreateInstance(
                 msdtype,
                 new object?[]
                 {
                     LookupFactory.DefineLookupFilter(typeof(TEntity), type, prefix, new TEntity[] { entity }),
-                    columns,
+                    cols,
                     false
                 }
             ) as IMultiSelectDialog;
@@ -171,7 +174,14 @@ public class DynamicGridCodePopupColumn<TEntity> : DynamicGridEditorColumn<TEnti
             {
                 var row = msd.Data().Rows.FirstOrDefault();
                 if (row != null)
-                    UpdateCodePopupColumnValue(row,prefix);
+                {
+                    row = ClientFactory.CreateClient(type)
+                        .Query(Filter.Create(type, "ID", Operator.IsEqualTo, row.Get<Guid>("ID")), columns)
+                        .Rows
+                        .FirstOrDefault();
+                    if (row != null)
+                        UpdateCodePopupColumnValue(row,prefix);
+                }
             }
         }
 

+ 79 - 51
inabox.wpf/DynamicGrid/Controls/MultiSelectDialog.cs

@@ -21,6 +21,7 @@ public interface IMultiSelectDialog
     CoreTable Data();
 
     Entity[] Items(IColumns? columns = null);
+    bool CanAdd { get; set; }
 }
 
 public class MultiSelectDialogSettings : BaseObject, IUserConfigurationSettings
@@ -43,27 +44,30 @@ public class MultiSelectDialog<T> : IMultiSelectDialog where T : Entity, IRemota
 
     private readonly Filter<T>? _filter;
     private readonly DynamicDataGrid<T> datagrid;
-    private readonly Grid grid;
-    private readonly Button ClearButton;
+    //private readonly Grid grid;
+    private readonly Button CancelButton;
     private readonly Button OKButton;
     private ThemableWindow? window;
     
+    public bool CanAdd { get; set; } = true;
+    
     public MultiSelectDialog(Filter<T>? filter, Columns<T>? columns, bool multiselect = true)
     {
         
         _filter = filter;
 
-        grid = new Grid();
-        grid.Margin = new Thickness(5);
-        grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1.0F, GridUnitType.Star) });
-        grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(40.0F, GridUnitType.Pixel) });
+        //grid = new Grid();
+        //grid.Margin = new Thickness(5);
+        //grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1.0F, GridUnitType.Star) });
+        //grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(40.0F, GridUnitType.Pixel) });
 
-        grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(80.0F, GridUnitType.Pixel) });
-        grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1.0F, GridUnitType.Star) });
-        grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(80.0F, GridUnitType.Pixel) });
-        grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(80.0F, GridUnitType.Pixel) });
+        //grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(80.0F, GridUnitType.Pixel) });
+        //grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1.0F, GridUnitType.Star) });
+        //grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(80.0F, GridUnitType.Pixel) });
+        //grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(80.0F, GridUnitType.Pixel) });
 
         datagrid = new DynamicDataGrid<T>();
+        datagrid.Margin = new Thickness(5);
         
         datagrid.Reconfigure(options =>
         {
@@ -73,9 +77,17 @@ public class MultiSelectDialog<T> : IMultiSelectDialog where T : Entity, IRemota
             options.FilterRows = true;
             if (multiselect)
                 options.MultiSelect = true;
+            options.AddRows = CanAdd;
+            options.RecordCount = true;
             options.EndUpdate();
         });
         datagrid.Reconfigure();
+        OKButton = datagrid.AddButton("OK", null, OKClick, DynamicGridButtonPosition.Right);
+        OKButton.Width = 80;
+        OKButton.IsEnabled = false;
+        
+        CancelButton = datagrid.AddButton("Cancel", null, CancelClick, DynamicGridButtonPosition.Right);
+        CancelButton.Width = 80;
         
         datagrid.OnReload += Grid_OnReload;
         datagrid.OnDoubleClick += Grid_DoubleClick;
@@ -107,33 +119,48 @@ public class MultiSelectDialog<T> : IMultiSelectDialog where T : Entity, IRemota
         datagrid.SetValue(Grid.ColumnProperty, 0);
         datagrid.SetValue(Grid.ColumnSpanProperty, 4);
         datagrid.OnSelectItem += Datagrid_OnSelectItem;
-        grid.Children.Add(datagrid);
+        //grid.Children.Add(datagrid);
         
-        ClearButton = new Button();
-        ClearButton.Margin = new Thickness(0, 5, 5, 0);
-        ClearButton.Content = "Clear";
-        ClearButton.Click += ClearButton_Click;
-        ClearButton.SetValue(Grid.RowProperty, 1);
-        ClearButton.SetValue(Grid.ColumnProperty, 0);
-        grid.Children.Add(ClearButton);
-
-        OKButton = new Button();
-        OKButton.Margin = new Thickness(5, 5, 0, 0);
-        OKButton.Content = "OK";
-        OKButton.Click += OKButton_Click;
-        OKButton.SetValue(Grid.RowProperty, 1);
-        OKButton.SetValue(Grid.ColumnProperty, 2);
-        OKButton.IsEnabled = false;
-        grid.Children.Add(OKButton);
+        // ClearButton = new Button();
+        // ClearButton.Margin = new Thickness(0, 5, 5, 0);
+        // ClearButton.Content = "Clear";
+        // ClearButton.Click += ClearButton_Click;
+        // ClearButton.SetValue(Grid.RowProperty, 1);
+        // ClearButton.SetValue(Grid.ColumnProperty, 0);
+        // grid.Children.Add(ClearButton);
+        //
+        // OKButton = new Button();
+        // OKButton.Margin = new Thickness(5, 5, 0, 0);
+        // OKButton.Content = "OK";
+        // OKButton.Click += OKButton_Click;
+        // OKButton.SetValue(Grid.RowProperty, 1);
+        // OKButton.SetValue(Grid.ColumnProperty, 2);
+        // OKButton.IsEnabled = false;
+        // grid.Children.Add(OKButton);
+        //
+        // var CancelButton = new Button();
+        // CancelButton.Margin = new Thickness(5, 5, 0, 0);
+        // CancelButton.Content = "Cancel";
+        // CancelButton.Click += CancelButton_Click;
+        // CancelButton.SetValue(Grid.RowProperty, 1);
+        // CancelButton.SetValue(Grid.ColumnProperty, 3);
+        // grid.Children.Add(CancelButton);
+    }
 
-        var CancelButton = new Button();
-        CancelButton.Margin = new Thickness(5, 5, 0, 0);
-        CancelButton.Content = "Cancel";
-        CancelButton.Click += CancelButton_Click;
-        CancelButton.SetValue(Grid.RowProperty, 1);
-        CancelButton.SetValue(Grid.ColumnProperty, 3);
-        grid.Children.Add(CancelButton);
+    private bool OKClick(Button button, CoreRow[] rows)
+    {
+        window!.DialogResult = true;
+        window!.Close();
+        return false;
+    }
+    
+    private bool CancelClick(Button button, CoreRow[] rows)
+    {
+        window!.DialogResult = false;
+        window!.Close();
+        return false;
     }
+
     public bool ShowDialog(string? column = null, string? value = null, FilterType filtertype = FilterType.Contains) =>
         ShowDialogInternal(null, column, value, filtertype);
     public bool ShowDialog(string title) =>
@@ -145,7 +172,7 @@ public class MultiSelectDialog<T> : IMultiSelectDialog where T : Entity, IRemota
         {
             Title = title ?? "Select Items",
             WindowStyle = WindowStyle.SingleBorderWindow,
-            Content = grid
+            Content = datagrid
         };
         datagrid.Refresh(true, true);
         if (!column.IsNullOrWhiteSpace() && !value.IsNullOrWhiteSpace())
@@ -245,22 +272,23 @@ public class MultiSelectDialog<T> : IMultiSelectDialog where T : Entity, IRemota
         OKButton.IsEnabled = e.Rows?.Any() == true;
     }
 
-    private void CancelButton_Click(object sender, RoutedEventArgs e)
-    {
-        window!.Close();
-    }
-
-    private void OKButton_Click(object sender, RoutedEventArgs e)
-    {
-        window!.DialogResult = true;
-        window.Close();
-    }
-
-    private void ClearButton_Click(object sender, RoutedEventArgs e)
-    {
-        window!.DialogResult = false;
-        window.Close();
-    }
+    // private void CancelButton_Click(object sender, RoutedEventArgs e)
+    // {
+    //     window!.DialogResult = false;
+    //     window!.Close();
+    // }
+    //
+    // private void OKButton_Click(object sender, RoutedEventArgs e)
+    // {
+    //     window!.DialogResult = true;
+    //     window.Close();
+    // }
+    //
+    // private void ClearButton_Click(object sender, RoutedEventArgs e)
+    // {
+    //     window!.DialogResult = false;
+    //     window.Close();
+    // }
 
     private void Grid_OnReload(object sender, Filters<T> criteria, Columns<T> columns, ref SortOrder<T>? sortby)
     {

+ 57 - 3
inabox.wpf/DynamicGrid/Grids/DynamicGrid.cs

@@ -6,6 +6,7 @@ using System.Diagnostics;
 using System.Globalization;
 using System.Linq;
 using System.Linq.Expressions;
+using System.Runtime.CompilerServices;
 using System.Threading.Tasks;
 using System.Windows;
 using System.Windows.Controls;
@@ -34,7 +35,7 @@ using System.Threading;
 
 namespace InABox.DynamicGrid;
 
-public abstract class DynamicGrid : ContentControl
+public abstract class DynamicGrid : ContentControl, INotifyPropertyChanged
 {
     public static readonly DependencyProperty UseWaitCursorProperty =
         DependencyProperty.Register(nameof(UseWaitCursor), typeof(bool), typeof(DynamicGrid<>));
@@ -44,6 +45,50 @@ public abstract class DynamicGrid : ContentControl
         get => (bool)GetValue(UseWaitCursorProperty);
         set => SetValue(UseWaitCursorProperty, value);
     }
+    
+    public static readonly DependencyProperty ItemsSourceProperty = 
+        DependencyProperty.Register(
+            nameof(ItemsSource),
+            typeof(object),
+            typeof(DynamicGrid),
+            new PropertyMetadata(null, DoItemsSourceChanged)
+        );
+
+    private static void DoItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
+    {
+        if (d is DynamicGrid dynamicGrid)
+        {
+            dynamicGrid.OnItemSourceChanged(e.NewValue);
+        }
+    }
+
+    public object? ItemsSource
+    {
+        get => GetValue(ItemsSourceProperty);
+        set => SetValue(ItemsSourceProperty, value);
+    }
+
+    public abstract void OnItemSourceChanged(object value);
+    
+    
+    #region INotifyPropertyChanged
+
+    public event PropertyChangedEventHandler? PropertyChanged;
+
+    protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
+    {
+        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
+    }
+
+    protected bool SetField<T>(ref T field, T value, [CallerMemberName] string? propertyName = null)
+    {
+        if (EqualityComparer<T>.Default.Equals(field, value)) return false;
+        field = value;
+        OnPropertyChanged(propertyName);
+        return true;
+    }
+    
+    #endregion
 }
 
 public abstract class DynamicGrid<T> : DynamicGrid, IDynamicGridUIComponentParent<T>, IDynamicGrid<T>
@@ -656,6 +701,12 @@ public abstract class DynamicGrid<T> : DynamicGrid, IDynamicGridUIComponentParen
 
     public CoreTable Data { get; set; }
 
+    public override void OnItemSourceChanged(object value)
+    {
+        Data = value as CoreTable;
+        Refresh(true, true);
+    }
+
     public class HiddenColumnsList
     {
         private List<string> Columns { get; set; } = new();
@@ -863,7 +914,8 @@ public abstract class DynamicGrid<T> : DynamicGrid, IDynamicGridUIComponentParen
 
         foreach (var (key, value) in changes)
         {
-            row[key] = value;
+            if (row.Table.GetColumnIndex(key) > -1)
+                row[key] = value;
         }
 
         UIComponent.UpdateRow(row);
@@ -1705,7 +1757,9 @@ public abstract class DynamicGrid<T> : DynamicGrid, IDynamicGridUIComponentParen
 
     BaseEditor IDynamicGridUIComponentParent<T>.CustomiseEditor(DynamicGridColumn column, BaseEditor editor)
     {
-        return editor.CloneEditor();
+        var result =  editor.CloneEditor();
+        CustomiseEditor(new T[] { }, column, result);
+        return result;
     }
 
     private void DoCustomiseEditor(IDynamicEditorForm sender, object[] items, DynamicGridColumn column, BaseEditor editor)

+ 11 - 0
inabox.wpf/DynamicGrid/Grids/DynamicItemsListGrid.cs

@@ -8,6 +8,7 @@ using System.Threading.Tasks;
 using Microsoft.CodeAnalysis.CSharp.Syntax;
 using Syncfusion.Windows.Tools.Controls;
 using System.Threading;
+using System.Windows;
 
 namespace InABox.DynamicGrid;
 
@@ -21,6 +22,7 @@ public interface IDynamicItemsListGrid : IDynamicGrid
     /// <b>Note:</b> This must be a list of type <see cref="List{T}"/>, otherwise the assignment to this property <u>will not</u> work.
     /// </remarks>
     IList Items { get; set; }
+    
 }
 
 public class DynamicItemsListGrid<T> : DynamicGrid<T>, IDynamicItemsListGrid
@@ -41,6 +43,15 @@ public class DynamicItemsListGrid<T> : DynamicGrid<T>, IDynamicItemsListGrid
         set => _items = value as List<T> ?? new List<T>();
     }
 
+    public override void OnItemSourceChanged(object value)
+    {
+        if (value is List<T> list)
+        {
+            Items = list;
+            Refresh(true,true);
+        }
+    }
+
     protected override void Init()
     {
     }