Procházet zdrojové kódy

Fixed DataModel setup for Master/Detail Panels

frogsoftware před 2 měsíci
rodič
revize
6ff5f53ec7
1 změnil soubory, kde provedl 35 přidání a 27 odebrání
  1. 35 27
      inabox.wpf/MasterDetailPanel/MasterDetailPanel.cs

+ 35 - 27
inabox.wpf/MasterDetailPanel/MasterDetailPanel.cs

@@ -21,7 +21,7 @@ public abstract class MasterDetailPanel<TMaster, TMasterGrid, TSettings> : Maste
     private readonly List<IMasterDetailPage<TMaster>> _pages = new();
     private DateTime _lastselection = DateTime.MaxValue;
     private int _currentPage = -1;
-    private readonly TMasterGrid _masterGrid;
+    public TMasterGrid MasterGrid { get; private set; }
     private IDataModelSource? _modelsource;
 
     protected TSettings Settings { get; private set; } = new();
@@ -39,7 +39,7 @@ public abstract class MasterDetailPanel<TMaster, TMasterGrid, TSettings> : Maste
         BeforeSaveSettings(Settings);
         Settings.AnchorWidth = _splitPanel.AnchorWidth;
         Settings.ViewType = _splitPanel.View;
-        Settings.MasterID = _masterGrid.SelectedRows.FirstOrDefault()?.Get<TMaster, Guid>(x => x.ID) ?? Guid.Empty;
+        Settings.MasterID = MasterGrid.SelectedRows.FirstOrDefault()?.Get<TMaster, Guid>(x => x.ID) ?? Guid.Empty;
         new UserConfiguration<TSettings>().Save(Settings);
     }
     
@@ -57,10 +57,11 @@ public abstract class MasterDetailPanel<TMaster, TMasterGrid, TSettings> : Maste
     {
         SaveSettings();
         var newTag = GetMasterColumnsTag();
-        if (!String.Equals(_masterGrid.ColumnsTag, newTag))
+        if (!String.Equals(MasterGrid.ColumnsTag, newTag))
         {
-            _masterGrid.ColumnsTag = newTag;
-            _masterGrid.Refresh(true, true);
+            MasterGrid.ColumnsTag = newTag;
+            MasterGrid.Refresh(true, true);
+            _lastselection = DateTime.Now;
         }
     }
 
@@ -74,19 +75,19 @@ public abstract class MasterDetailPanel<TMaster, TMasterGrid, TSettings> : Maste
 
     protected MasterDetailPanel()
     {
-        _masterGrid = new TMasterGrid();
-        _masterGrid.OnSelectItem += MasterGrid_OnSelectItem;
-        _masterGrid.BeforeRefresh += MasterGrid_BeforeRefresh;
-        _masterGrid.AfterRefresh += MasterGrid_AfterRefresh;
+        MasterGrid = new TMasterGrid();
+        MasterGrid.OnSelectItem += MasterGrid_OnSelectItem;
+        MasterGrid.BeforeRefresh += MasterGrid_BeforeRefresh;
+        MasterGrid.AfterRefresh += MasterGrid_AfterRefresh;
     }
 
     public void Setup()
     {
         LoadSettings();
         
-        _masterGrid.ColumnsTag = GetMasterColumnsTag();
+        MasterGrid.ColumnsTag = GetMasterColumnsTag();
         
-        _splitPanel.Master = _masterGrid;
+        _splitPanel.Master = MasterGrid;
         _splitPanel.MasterCaption = MasterCaption;
         _splitPanel.DetailCaption = DetailCaption;
 
@@ -142,7 +143,7 @@ public abstract class MasterDetailPanel<TMaster, TMasterGrid, TSettings> : Maste
     
     public void Refresh()
     {
-        _masterGrid.Refresh(timer?.IsEnabled == false, true);
+        MasterGrid.Refresh(timer?.IsEnabled == false, true);
         _lastselection = DateTime.MinValue;
         if (timer != null)
             timer.IsEnabled = true;
@@ -152,11 +153,8 @@ public abstract class MasterDetailPanel<TMaster, TMasterGrid, TSettings> : Maste
     {
         if (_modelsource == null)
         {
-            var row = _masterGrid.SelectedRows.FirstOrDefault();
-            var filter = row != null
-                ? new Filter<TMaster>(x => x.ID).IsEqualTo(row.Get<TMaster, Guid>(x => x.ID))
-                : new Filter<TMaster>().None();
-            return new AutoDataModel<TMaster>(filter);
+            var ids = MasterGrid.ExtractValues(x => x.ID, Selection.Selected).ToArray();
+            return new AutoDataModel<TMaster>(new Filter<TMaster>(x => x.ID).InList(ids));
         }
 
         return _modelsource.DataModel(selection);
@@ -172,7 +170,7 @@ public abstract class MasterDetailPanel<TMaster, TMasterGrid, TSettings> : Maste
     {
         return SelectedPage != null
            ? SelectedPage.Selected()
-           : new Dictionary<string, object[]> { { typeof(TMaster).EntityName(), _masterGrid.SelectedRows } };
+           : new Dictionary<string, object[]> { { typeof(TMaster).EntityName(), MasterGrid.SelectedRows } };
     }
 
     public void Heartbeat(TimeSpan time)
@@ -191,16 +189,26 @@ public abstract class MasterDetailPanel<TMaster, TMasterGrid, TSettings> : Maste
         if (_lastselection < DateTime.Now.AddMilliseconds(-500) && !_masterGridRefreshing)
         {
             _lastselection = DateTime.MaxValue;
-            var master = _masterGrid.SelectedRows.FirstOrDefault()?.ToObject<TMaster>() ?? new TMaster();
-            var dataModelSource = SelectedPage?.Refresh(master);
-            if (_tabControl.SelectedIndex != _currentPage)
+            if (_splitPanel.View == DynamicSplitPanelView.Master)
+            {
+                _modelsource = null;
+                OnUpdateDataModel?.Invoke(this.SectionName, this.DataModel(Selection.None));
+            }
+            else
             {
-                if(dataModelSource is not null)
+                var master = MasterGrid.SelectedRows.FirstOrDefault()?.ToObject<TMaster>() ?? new TMaster();
+                var dataModelSource = SelectedPage?.Refresh(master);
+                if (_tabControl.SelectedIndex != _currentPage)
                 {
-                    _modelsource = dataModelSource;
-                    OnUpdateDataModel?.Invoke(dataModelSource.SectionName, dataModelSource.DataModel(Selection.None));
+                    if (dataModelSource is not null)
+                    {
+                        _modelsource = dataModelSource;
+                        OnUpdateDataModel?.Invoke(dataModelSource.SectionName,
+                            dataModelSource.DataModel(Selection.None));
+                    }
+
+                    _currentPage = _tabControl.SelectedIndex;
                 }
-                _currentPage = _tabControl.SelectedIndex;
             }
         }
     }
@@ -215,9 +223,9 @@ public abstract class MasterDetailPanel<TMaster, TMasterGrid, TSettings> : Maste
     private void MasterGrid_AfterRefresh(object sender, AfterRefreshEventArgs args)
     {
 
-        _masterGrid.SelectedRows = Settings.MasterID == Guid.Empty
+        MasterGrid.SelectedRows = Settings.MasterID == Guid.Empty
             ? Array.Empty<CoreRow>()
-            : _masterGrid.Data.Rows.Where(r => r.Get<TMaster, Guid>(c => c.ID) == Settings.MasterID).ToArray();
+            : MasterGrid.Data.Rows.Where(r => r.Get<TMaster, Guid>(c => c.ID) == Settings.MasterID).ToArray();
         _masterGridRefreshing = false;
     }