#region Copyright Syncfusion Inc. 2001-2024. // Copyright Syncfusion Inc. 2001-2024. All rights reserved. // Use of this code is subject to the terms of our license. // A copy of the current license can be obtained at any time by e-mailing // licensing@syncfusion.com. Any infringement will be prosecuted under // applicable laws. #endregion using Microsoft.Xaml.Behaviors; using Syncfusion.SfSkinManager; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; namespace syncfusion.demoscommon.wpf { public class ScrollToSelectedItemAction : Behavior { /// /// Called after the behavior is attached to an AssociatedObject. /// protected override void OnAttached() { base.OnAttached(); this.AssociatedObject.Loaded += AssociatedObject_Loaded; this.AssociatedObject.SelectionChanged += AssociatedObject_SelectionChanged; } /// /// Occurs when the demolist is loaded /// /// /// void AssociatedObject_Loaded(object sender, RoutedEventArgs e) { ScrollSelectedIntoView(sender); } /// /// Occurs when the selection of product demos changes /// void AssociatedObject_SelectionChanged(object sender, SelectionChangedEventArgs e) { ScrollSelectedIntoView(sender); } /// /// Scrolls the selected item into view for a ListView control if it exists. /// /// private void ScrollSelectedIntoView(object sender) { if (sender is ListView demolist && demolist.SelectedItem != null) { demolist.ScrollIntoView(demolist.SelectedItem); } } /// /// Called when the behavior is being detached from its AssociatedObject, but before it has actually occurred. /// protected override void OnDetaching() { base.OnDetaching(); this.AssociatedObject.Loaded -= AssociatedObject_Loaded; this.AssociatedObject.SelectionChanged -= AssociatedObject_SelectionChanged; } } }