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

元气小坏坏 提交于 2019-12-19 08:56:05

问题


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 when I reach the top and pull down I would like to see some text like "Scroll down more to retrieve more items" and if you do so it then triggers a data download for items that occur earlier.

Same thing for the bottom item if you scoll down far enough (beneath the last item it triggers a data download for items later than listed).

I'm not after a seamless listbox I want the data download to trigger only if I scroll upp or down far enough and release. I hope this description makes sense.

Basically what I need to know is the exact scroll position in the Listbox and so that I can detemine when the list has been dragged and released far down or up. For those who have IPhone the effect I'm after is used in the Facebook app.

Cheers

/Jimmy


回答1:


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.




回答2:


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;
}


来源:https://stackoverflow.com/questions/4951812/how-do-i-get-the-exact-scroll-position-in-an-listbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!