A behavior to keep ListView's SelectedItem into View

From Yggenyk
Jump to navigation Jump to search


<syntaxhighlight lang=CSharp> using Windows.UI.Xaml.Controls; using Microsoft.Xaml.Interactivity;

namespace TrackMan.Soccer.UWP.Behaviors {

   public class ScrollSelectedItemIntoViewBehavior : Behavior<ListView>
   {
       protected override void OnAttached()
       {
           base.OnAttached();
           AssociatedObject.SelectionChanged += ListView_SelectionChanged;
       }
       protected override void OnDetaching()
       {
           base.OnDetaching();
           AssociatedObject.SelectionChanged -= ListView_SelectionChanged;
       }
       private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
       {
           if (AssociatedObject.SelectedItem != null)
               AssociatedObject.ScrollIntoView(AssociatedObject.SelectedItem);
           else if (AssociatedObject.SelectedItems != null && AssociatedObject.SelectedItems.Count > 0)
               AssociatedObject.ScrollIntoView(AssociatedObject.SelectedItems[0]);
       }
   }

} </syntaxhighlight>

id=siteTree