user975343
user975343

Reputation:

Detect When User performs Vertical Scroll

I'm using a Pivot in one of my windows phone application screens, and I'd like to detect whether a user is performing a vertical scroll. I noticed that there is no built-in event for detecting it, and I also noticed that ScrollViewer,Grid etc does not have the Scroll property. I'd like to know if it is possible to detect a vertical scroll. I'd be grateful if anyone could point me to the solution. Thanks in advance !

<ScrollViewer Name="mailSV">
    <controls:Pivot Name="mailPivot" Title="EyeLight">
        <!--Pivot item one-->
        <controls:PivotItem Name="GmailPivot" Header="Gmail">
            <!--Double line list with text wrapping-->
            <Button Name="Gmail" Tap="mailSingleTap" DoubleTap="listenMode" FontSize="120" Foreground="Black">
                <Button.Background>
                    <ImageBrush ImageSource="/ScrollingApp;component/Images/Gmail-icon.png" Stretch="Uniform" />
                </Button.Background>
            </Button>
        </controls:PivotItem>

            <!--Pivot item two-->
        <controls:PivotItem Name="YahooPivot" Header="Yahoo">
            <!--Triple line list no text wrapping-->
            <Button Name="Yahoo" FontSize="120" Tap="mailSingleTap" DoubleTap="listenMode" Foreground="Black">
                <Button.Background>
                    <ImageBrush ImageSource="/ScrollingApp;component/Images/yahoo2.jpeg" Stretch="Uniform" />
                </Button.Background>
            </Button>
        </controls:PivotItem>
    </controls:Pivot>
</ScrollViewer>

Upvotes: 0

Views: 129

Answers (1)

Mahima Gandhe
Mahima Gandhe

Reputation: 651

Try this, It worked for me in case of a list box.

Define the following method and property;

ScrollViewer scrollViewer;

 private static void OnListVerticalOffsetChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
            {
                //Create an object of the same class(page).      
                MyPage page = obj as MyPage ;

                ScrollViewer viewer = page.scrollViewer;

                //Checks if the Scroll has reached the last item based on the ScrollableHeight
                bool atBottom = viewer.VerticalOffset >= viewer.ScrollableHeight;

                if (atBottom)
                {
                    //Type your Code here after checking the vertical scroll.               
                }
            }

Now use the following code to get the value of ListVerticalOffset by using above method.

public readonly DependencyProperty ListVerticalOffsetProperty = DependencyProperty.Register("ListVerticalOffset", typeof(double), typeof(ImageSearch),
                new PropertyMetadata(new PropertyChangedCallback(OnListVerticalOffsetChanged)));

Now bind this property to your current instance on the Loaded event of Your ListBox.

void listBox_Loaded(object sender, RoutedEventArgs e)
        {

            FrameworkElement element = (FrameworkElement)sender;
            element.Loaded -= LstImage_Loaded;
            scrollViewer = FindChildOfType<ScrollViewer>(element);
            if (scrollViewer == null)
            {
                throw new InvalidOperationException("ScrollViewer not found.");
            }

            Binding binding = new Binding();
            binding.Source = scrollViewer;
            binding.Path = new PropertyPath("VerticalOffset");
            binding.Mode = BindingMode.OneWay;
            this.SetBinding(ListVerticalOffsetProperty, binding);
        }

Upvotes: 1

Related Questions