using System; using System.Reactive.Disposables; using Avalonia.Controls; namespace Avalonia.Xaml.Interactions.Custom; /// /// /// public class ScrollToItemBehavior : AttachedToVisualTreeBehavior { /// /// /// public static readonly StyledProperty?> ItemProperty = AvaloniaProperty.Register?>(nameof(Item)); /// /// /// public IObservable? Item { get => GetValue(ItemProperty); set => SetValue(ItemProperty, value); } /// /// /// /// protected override void OnAttachedToVisualTree(CompositeDisposable disposables) { var disposable = Item?.Subscribe(item => { AssociatedObject?.ScrollIntoView(item); }); if (disposable is not null) { disposables.Add(disposable); } } }