Lazy loading of non-visible elements

后端 未结 2 1685
Happy的楠姐
Happy的楠姐 2021-01-06 14:29

I have a case where I have either a gridview/listbox/any type of items control and the number of items bound to the control is massive (easily arou

2条回答
  •  难免孤独
    2021-01-06 15:23

    Here is an event that will notify when user scrolls into the last screen of data:

    using System.Windows;
    using System.Windows.Controls;
    
    public static class ScrollViewer
    {
        public static readonly RoutedEvent LastPageEvent = EventManager.RegisterRoutedEvent(
            "LastPage",
            RoutingStrategy.Bubble,
            typeof(RoutedEventHandler),
            typeof(ScrollViewer));
    
        private static readonly RoutedEventArgs EventArgs = new RoutedEventArgs(LastPageEvent);
    
        static ScrollViewer()
        {
            EventManager.RegisterClassHandler(
                typeof(System.Windows.Controls.ScrollViewer),
                System.Windows.Controls.ScrollViewer.ScrollChangedEvent,
                new ScrollChangedEventHandler(OnScrollChanged));
        }
        public static void AddLastPageHandler(UIElement e, RoutedEventHandler handler)
        {
            e.AddHandler(LastPageEvent, handler);
        }
    
        public static void RemoveLastPageHandler(UIElement e, RoutedEventHandler handler)
        {
            e.RemoveHandler(LastPageEvent, handler);
        }
    
        private static void OnScrollChanged(object sender, ScrollChangedEventArgs e)
        {
            if (e.ViewportHeight == 0 || e.VerticalOffset == 0)
            {
                return;
            }
    
            var verticalSpaceLeft = e.ExtentHeight - e.VerticalOffset;
            if (verticalSpaceLeft < 2 * e.ViewportHeight)
            {
                var scrollViewer = (System.Windows.Controls.ScrollViewer)sender;
                scrollViewer.RaiseEvent(EventArgs);
            }
        }
    }
    

提交回复
热议问题