Windows Phone 7 - ScrollViewer value changed

筅森魡賤 提交于 2019-12-06 03:40:42

It is possible by using the scrollviewers dependency properties, it has a HorizontalOffset and a VerticalOffset. The trick is to bind event to the scrollviewer, but it can bee done in the load event handler. If you put a wide grid in your scrollviewer you can get the offset!

In your xaml file (MainPage sample here):

        <ScrollViewer Loaded="ScrollViewer_Loaded_1">
        <Grid x:Name="ContentPanel" Grid.Row="1" Width="1000" Margin="12,0,12,0">
            <StackPanel>
                ...

In your code behind file (MainPage.cs here):

        public static readonly DependencyProperty ScrollViewVerticalOffsetProperty =
        DependencyProperty.Register(
                                    "ScrollViewVerticalOffset",
                                    typeof(double),
                                    typeof(MainPage),
                                    new PropertyMetadata(new PropertyChangedCallback(OnScrollViewVerticalOffsetChanged))
                                    );

        public static readonly DependencyProperty ScrollViewHorizontalOffsetProperty =
        DependencyProperty.Register(
                                    "ScrollViewHorizontalOffset",
                                    typeof(double),
                                    typeof(MainPage),
                                    new PropertyMetadata(new PropertyChangedCallback(OnScollViewHorizontalOffsetChanged))
                                    );

    private ScrollViewer _listScrollViewer;

    private void ScrollViewer_Loaded_1(object sender, RoutedEventArgs e)
    {
        _listScrollViewer = sender as ScrollViewer;

        Binding binding1 = new Binding();
        binding1.Source = _listScrollViewer;
        binding1.Path = new PropertyPath("VerticalOffset");
        binding1.Mode = BindingMode.OneWay;
        this.SetBinding(ScrollViewVerticalOffsetProperty, binding1);

        Binding binding2 = new Binding();
        binding2.Source = _listScrollViewer;
        binding2.Path = new PropertyPath("HorizontalOffset");
        binding2.Mode = BindingMode.OneWay;
        this.SetBinding(ScrollViewHorizontalOffsetProperty, binding2);
    }

    public double ScrollViewVerticalOffset
    {
        get { return (double)this.GetValue(ScrollViewVerticalOffsetProperty); }
        set { this.SetValue(ScrollViewVerticalOffsetProperty, value); }
    }

    public double ScrollViewHorizontalOffset
    {
        get { return (double)this.GetValue(ScrollViewHorizontalOffsetProperty); }
        set { this.SetValue(ScrollViewHorizontalOffsetProperty, value); }
    }

    private static void OnScrollViewVerticalOffsetChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
        MainPage page = obj as MainPage;
        ScrollViewer viewer = page._listScrollViewer;

        // ... do something here
    }

    private static void OnScollViewHorizontalOffsetChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
        MainPage page = obj as MainPage;
        ScrollViewer viewer = page._listScrollViewer;

        // ... do something here
    }

here's the XAML code I used

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0" LayoutUpdated='ContentPanel_LayoutUpdated'>

        <ScrollViewer x:Name='scroller' VerticalAlignment='Stretch' VerticalScrollBarVisibility='Visible' >
            <StackPanel
                x:Name='listItems'></StackPanel>
        </ScrollViewer>
    </Grid>

and here's the C# code behind

private void ContentPanel_LayoutUpdated(object sender, EventArgs e)
    {
        var offset = scroller.VerticalOffset;
    }

whenever the scroller is scrolled then the layout of the Grid (Container grid) changes so layout updated event is fired ... please try debugging by placing break point inside the event and look for the offset value ..

Add the property ManipulationMode="Control" to your ScrollViewer. This is needed because otherwise the UI thread will not be notified with enough ScrollViewer scroll values to get a fluid animation – the normal mode is a performance optimization from Windows Phone that you need to bypass!

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