Windows Phone 7的 - 的ScrollViewer值改变 [英] Windows Phone 7 - ScrollViewer value changed

查看:139
本文介绍了Windows Phone 7的 - 的ScrollViewer值改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找所有的时间解决方案,并不能得到正确的。
我有网格,具有宽度960,并有的ScrollViewer 在里面。现在我想知道我的滚动值(水平偏移),而滚动。所有这一切我发现的解决方案是为WPF / Silverlight和它不会为我工作。

I am searching all the time for solution and cant get correct one. I have grid that have width 960 and have ScrollViewer in it. Now i would like to know value (horizontal offset) of my scroll while scrolling. All solutions that i am finding is for wpf/silverlight and it wont works for me.

修改

确定,这里是示例代码,XAML:

Ok, here is the example 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>



C#

c#

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



但问题是,它是在不改变大小的所有时间。也许我的英语不是很好,你不能uderstand我。这是电影的例子,我滑左和右的大小始终是相同的。当我停止滑动它正在改变大小。

But the problem is that it is not changing size all the time. Maybe my English is not well and you cant uderstand me. Here is movie example, i am sliding left right and the size is always the same. When i stop sliding it is changing size.

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

推荐答案

据可以通过使用scrollviewers依赖属性,它有一个Horizo​​ntalOffset和VerticalOffset。诀窍是事件绑定到的ScrollViewer,但它可以在加载事件处理程序蜜蜂完成。 !如果你把一个宽网格在你的ScrollViewer你可以得到弥补。

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!

在您的XAML文件(这里的MainPage样品):

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>
                ...

在你的代码隐藏文件(MainPage.cs这里):

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
    }

这篇关于Windows Phone 7的 - 的ScrollViewer值改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆