How do I get the exact scroll position in an Listbox?

后端 未结 2 972
执笔经年
执笔经年 2021-01-13 14:49

I would like to be able to load more data into a listbox when I reach the begining or the end of the listbox.

My listbox shows items that are time related therefor w

相关标签:
2条回答
  • 2021-01-13 15:13

    To get the exact scroll position you need to get at the ListBox's ScrollViewer. I use this for Pivots (to return the the previous scrolling position after tombstoning) - I assume it will work for ListBoxes too..

    var scrollViewer = FindScrollViewer(listBox);
    var offset = scrollViewer.VerticalOffset;
    
    static ScrollViewer FindScrollViewer(DependencyObject parent)
    {
        var childCount = VisualTreeHelper.GetChildrenCount(parent);
        for (var i = 0; i < childCount; i++)
        {
            var elt = VisualTreeHelper.GetChild(parent, i);
            if (elt is ScrollViewer) return (ScrollViewer)elt;
            var result = FindScrollViewer(elt);
            if (result != null) return result;
        }
        return null;
    }
    
    0 讨论(0)
  • 2021-01-13 15:25

    You're looking for two separate things. First, you need to change the default layout of the ListBox to have an ending control. A pretty decent implementation was presented here - you are basically inserting an extra control after the ItemPresenter in the default template.

    Looking for dynamic loading, here is an implementation.

    0 讨论(0)
提交回复
热议问题