Pointless Waymarks Tools

Artifact [02158d9558]
Login

Artifact [02158d9558]

Artifact 02158d95583ab73dfedb2b148b6665d6e5d4c8ede3b66e8a0b1c3a2c90976359:


using System.Collections.Specialized;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
using Microsoft.Xaml.Behaviors;

namespace PointlessWaymarks.WpfCommon.Behaviors;

public class ListBoxAutoScrollToNewItems : Behavior<ListBox>
{
    private INotifyCollectionChanged? _cachedItemsSource;

    private void AssociatedObjectLoaded(object sender, RoutedEventArgs e)
    {
        UpdateItemsSource();
    }

    private void ItemsSourceCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
    {
        if (e.Action == NotifyCollectionChangedAction.Add && e.NewItems?[0] != null)
            try
            {
                Application.Current?.Dispatcher?.BeginInvoke((Action) (() =>
                {
                    AssociatedObject.ScrollIntoView(e.NewItems[0]!);
                    AssociatedObject.SelectedItem = e.NewItems[0];
                }), DispatcherPriority.DataBind);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
    }

    protected override void OnAttached()
    {
        base.OnAttached();

        UpdateItemsSource();

        AssociatedObject.Loaded += AssociatedObjectLoaded;
    }

    private void UpdateItemsSource()
    {
        if (AssociatedObject.ItemsSource is INotifyCollectionChanged sourceCollection)
        {
            if (_cachedItemsSource != null) _cachedItemsSource.CollectionChanged -= ItemsSourceCollectionChanged;
            _cachedItemsSource = sourceCollection;
            if (_cachedItemsSource == null) return;
            _cachedItemsSource.CollectionChanged += ItemsSourceCollectionChanged;
        }
    }
}