开发者

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

开发者 https://www.devze.com 2023-02-09 08:48 出处:网络
I would like to be able to load more data into a listbox when I reach the begining or the end of the listbox.

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


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


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号