Search code examples
c#windows-phone-8longlistselectorpull-to-refresh

WP8 longlist selector PullToRefresh and null viewport


I'm trying to implement PullToRefresh on my longlistselector. I've wrote my code with the help of posts like this and this

the problem is that the compression event handler is called whenever I scroll up or down, regardless whether I've reached the top or bottom of list or not. By doing more search, I've found that my solution lays with viewportcontrol which is a child element of longlistselector. I tried getting this viewportcontrol using the VisualTreeHelper:

_viewport = FindVisualChild<ViewportControl>(listbox);

and by FindName :

_viewport  = this.FindName("ViewportControl") as ViewportControl;

but it's always returning null. Does anyone know what I'm doing wrong here?


Solution

  • I've solved my problem by creating a custom longlistselector and overriding the OnApplyTemplate method:

    public class MyLongListSelector : LongListSelector
        {
            public ViewportControl ViewportControl { get; private set; }
            public override void OnApplyTemplate()
            {
                base.OnApplyTemplate();
                ViewportControl = (ViewportControl)GetTemplateChild("ViewportControl");
            }
        }
    

    Hope this helps anyone.