Windows Phone 7 - ScrollViewer value changed

I'm looking all the time for a solution and can't get the right one. I have a grid 960 wide and have ScrollViewer

in it. Now I would like to know the value (horizontal offset) of my scroll while scrolling. All solutions I find are for wpf / silverlight and it doesn't work for me.

Edit

Ok, here's some sample code, xaml:

<ScrollViewer Name="Scroll" LayoutUpdated="ScrollViewer_LayoutUpdated" IsEnabled="True" Width="480" ScrollViewer.HorizontalScrollBarVisibility="Auto">
    <Grid x:Name="ContentPanel" Background="Red" Margin="12,0,12,0" Width="960">
        <Rectangle Name="GreenRectangle" Fill="Green" Width="240" Height="240"></Rectangle>
    </Grid>
</ScrollViewer>

      

FROM#

private void ScrollViewer_LayoutUpdated(object sender, EventArgs e)
{
    GreenRectangle.Width = Scroll.HorizontalOffset;
    GreenRectangle.Height = Scroll.HorizontalOffset;
}

      

But the problem is that it doesn't resize all the time. Perhaps my English is not very good and you cannot persuade me. Here's an example of a movie, I'm sliding left and left and the size is always the same. When I stop sliding, it changes size.

https://www.dropbox.com/s/eh28oavxpsy19bw/20130122_1601_56.avi

+3


source to share


3 answers


This is possible with the scrollviewers dependency properties, it has HorizontalOffset and VerticalOffset. The trick is to bind the event to the scrollviewer, but this can be done in the load event handler. If you put a wide grid on your scrollviewer you can get the offset!

In your xaml file (MainPage example 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 the 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
    }

      

+4


source


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 scroll of the scroller is scrolled, the grid layout (container grid) changes so that the updated event is updated ... try debugging by placing a breakpoint inside the event and find the offset value.

0


source


Add the ManipulationMode = "Control" property to your ScrollViewer. This is necessary because otherwise the UI thread will not be notified with enough ScrollViewer scrolling to get the current animation - normal mode is a performance optimization with Windows Phone that needs to be bypassed!

0


source







All Articles