Files
LogViewerControl/CSharp/Controls/LogViewer.Avalonia/LogViewerControl.axaml.cs
T
2026-04-05 10:34:49 +02:00

45 lines
1.2 KiB
C#

using System.Collections.Specialized;
using Avalonia.Controls;
using Avalonia.LogicalTree;
using Serilog.Sinks.LogView.Core.Logging;
namespace LogViewer.Avalonia;
public partial class LogViewerControl : UserControl
{
public LogViewerControl()
{
InitializeComponent();
}
private ILogDataStoreCore? vm;
private LogModel? item;
private void OnDataContextChanged(object? sender, EventArgs e)
{
if (DataContext is null)
return;
vm = (ILogDataStoreCore)DataContext;
vm.DataStore.Entries.CollectionChanged += OnCollectionChanged;
}
private void OnCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
=> item = MyDataGrid.ItemsSource.Cast<LogModel>().LastOrDefault();
private void OnLayoutUpdated(object? sender, EventArgs e)
{
if (CanAutoScroll.IsChecked != true || item is null)
return;
MyDataGrid.ScrollIntoView(item, null);
item = null;
}
private void OnDetachedFromLogicalTree(object? sender, LogicalTreeAttachmentEventArgs e)
{
if (vm is null) return;
vm.DataStore.Entries.CollectionChanged -= OnCollectionChanged;
}
}