Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the exact scroll position in an 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

like image 494
Jimmy Engtröm Avatar asked Dec 04 '22 09:12

Jimmy Engtröm


1 Answers

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;
}
like image 87
Damian Avatar answered Dec 05 '22 22:12

Damian