2
0
mirror of https://github.com/9ParsonsB/Pulsar.git synced 2025-04-05 17:39:39 -04:00

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.
This commit is contained in:
Fred Kuipers 2021-06-21 20:04:17 -04:00
parent 6902361f00
commit 2d672e9c2a
3 changed files with 28 additions and 8 deletions

View File

@ -29,8 +29,6 @@ namespace Observatory.UI.ViewModels
public BasicUIViewModel(ObservableCollection<object> BasicUIGrid)
{
this.BasicUIGrid = new();
this.BasicUIGrid = BasicUIGrid;
}

View File

@ -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);
}

View File

@ -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<Panel>("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<object>))
return;
var dataContext = (ObservableCollection<object>)sender;
dataGrid.ScrollIntoView(dataContext[dataContext.Count - 1], null);
}
private Grid GenerateCoreUI()