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()