From 2d672e9c2a6b23f8faca478f838d3554b213b4e3 Mon Sep 17 00:00:00 2001 From: Fred Kuipers Date: Mon, 21 Jun 2021 20:04:17 -0400 Subject: [PATCH] Implement scorlling BasicUIView grids to the bottom plus refactoring Upon arrival of new records to the backing ObservableCollection, scroll the DataGrid to the last item. However, when the DataGrid is initialized, there's not yet a data context set, so when that happens, then we can listen to the CollectionChanged event to trigger scrolling (only really needed for additions). In passing, also set the DataGrid to ReadOnly. In the other two files, minor simplifications/cleanup. --- .../UI/ViewModels/BasicUIViewModel.cs | 2 -- .../UI/ViewModels/CoreViewModel.cs | 4 +-- ObservatoryCore/UI/Views/BasicUIView.axaml.cs | 30 +++++++++++++++++-- 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/ObservatoryCore/UI/ViewModels/BasicUIViewModel.cs b/ObservatoryCore/UI/ViewModels/BasicUIViewModel.cs index 570a1a4..4926dff 100644 --- a/ObservatoryCore/UI/ViewModels/BasicUIViewModel.cs +++ b/ObservatoryCore/UI/ViewModels/BasicUIViewModel.cs @@ -29,8 +29,6 @@ namespace Observatory.UI.ViewModels public BasicUIViewModel(ObservableCollection BasicUIGrid) { - - this.BasicUIGrid = new(); this.BasicUIGrid = BasicUIGrid; } diff --git a/ObservatoryCore/UI/ViewModels/CoreViewModel.cs b/ObservatoryCore/UI/ViewModels/CoreViewModel.cs index 1d9bd39..3b8f166 100644 --- a/ObservatoryCore/UI/ViewModels/CoreViewModel.cs +++ b/ObservatoryCore/UI/ViewModels/CoreViewModel.cs @@ -30,12 +30,10 @@ namespace Observatory.UI.ViewModels { CoreModel coreModel = new(); coreModel.Name = worker.ShortName; - coreModel.UI = new(); - var uiViewModel = new BasicUIViewModel(worker.PluginUI.DataGrid) + coreModel.UI = new BasicUIViewModel(worker.PluginUI.DataGrid) { UIType = worker.PluginUI.PluginUIType }; - coreModel.UI = uiViewModel; tabs.Add(coreModel); } diff --git a/ObservatoryCore/UI/Views/BasicUIView.axaml.cs b/ObservatoryCore/UI/Views/BasicUIView.axaml.cs index 1772849..b57c631 100644 --- a/ObservatoryCore/UI/Views/BasicUIView.axaml.cs +++ b/ObservatoryCore/UI/Views/BasicUIView.axaml.cs @@ -8,11 +8,15 @@ using Observatory.Framework.Interfaces; using System.Linq; using Avalonia.Interactivity; using Avalonia.VisualTree; +using System; +using System.Collections.ObjectModel; namespace Observatory.UI.Views { public class BasicUIView : UserControl { + private DataGrid dataGrid; + public BasicUIView() { Initialized += OnInitialized; @@ -65,20 +69,22 @@ namespace Observatory.UI.Views private void UITypeChange() { var uiPanel = this.Find("UIPanel"); - + dataGrid = null; switch (UIType) { case PluginUI.UIType.None: break; case PluginUI.UIType.Basic: - DataGrid dataGrid = new() + dataGrid = new() { [!DataGrid.ItemsProperty] = new Binding("BasicUIGrid"), SelectionMode = DataGridSelectionMode.Extended, GridLinesVisibility = DataGridGridLinesVisibility.Vertical, - AutoGenerateColumns = true + AutoGenerateColumns = true, + IsReadOnly = true, }; dataGrid.AutoGeneratingColumn += ColumnGeneration; + dataGrid.DataContextChanged += OnDataContextSet; uiPanel.Children.Clear(); uiPanel.Children.Add(dataGrid); break; @@ -93,8 +99,26 @@ namespace Observatory.UI.Views default: break; } + } + private void OnDataContextSet(object sender, EventArgs e) + { + if (UIType != PluginUI.UIType.Basic || !(sender is DataGrid)) return; + dataGrid = (DataGrid)sender; + if (dataGrid.DataContext != null) + { + var dataContext = ((ViewModels.BasicUIViewModel)dataGrid.DataContext).BasicUIGrid; + dataContext.CollectionChanged += ScrollToLast; + } + } + private void ScrollToLast(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) + { + // Only trigger on adds. + if (e.Action != System.Collections.Specialized.NotifyCollectionChangedAction.Add || UIType != PluginUI.UIType.Basic || dataGrid == null || !(sender is ObservableCollection)) + return; + var dataContext = (ObservableCollection)sender; + dataGrid.ScrollIntoView(dataContext[dataContext.Count - 1], null); } private Grid GenerateCoreUI()