为什么我的scrollviewer不能在WP7中工作?

时间:2011-07-14 04:09:09

标签: windows-phone-7

我有一个滚动查看器,里面有一个列表框。我想让滚动查看器每次定时器勾选时使用ScrollToHorizo​​ntalOffset自动滚动(每1秒滚动100)。 但是它不起作用,计时器工作正常但滚动查看器不会移动。这是我的代码,请帮忙!

    DispatcherTimer timer = new DispatcherTimer();
    double current = 0;
   // Constructor
    public MainPage()
    {
        InitializeComponent();
        timer = new DispatcherTimer();
        this.imagesScrollview.Loaded += new RoutedEventHandler(imagesScrollview_Loaded);
        timer.Interval = TimeSpan.FromSeconds(1);

        timer.Tick += new EventHandler(timer_Tick);
        timer.Start();
    }
    void timer_Tick(object sender, EventArgs e)
    {
     //   ScrollViewer sv = new ScrollViewer();
        imagesScrollview.InvalidateScrollInfo();
        imagesScrollview.ScrollToHorizontalOffset(current);
        imagesScrollview.UpdateLayout();            
        current = current + 100;
        textBlock2.Text = current.ToString();           

    }

和我的scrollviewer:

 <ScrollViewer HorizontalScrollBarVisibility="Auto" Margin="8,563,8,2" Width="auto" x:Name="imagesScrollview" Opacity="1" Background="#FF3ED216" Grid.Row="1" RenderTransformOrigin="0.5,0.5" Loaded="imagesScrollview_Loaded">
        <ScrollViewer.RenderTransform>
            <CompositeTransform/>
        </ScrollViewer.RenderTransform>
        <ListBox x:Name="listBox" Width="Auto" Height="Auto" Background="#FF3ED216" ManipulationCompleted="listBox_ManipulationCompleted">
            <ListBox.RenderTransform>
                <CompositeTransform/>
            </ListBox.RenderTransform>
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal">
                        <StackPanel.RenderTransform>
                            <TranslateTransform X="0" />
                        </StackPanel.RenderTransform>
                    </StackPanel>
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal" Margin="15,0">
                        <Image x:Name="imageAV" Source="{Binding avlink}" Height="80" Width="80" Stretch="UniformToFill" MouseLeftButtonUp="imageAV_MouseLeftButtonUp" ImageFailed="imageAV_ImageFailed" />
                        <StackPanel Orientation="Vertical" Margin="10,0,0,0" MouseLeftButtonUp="StackPanel_MouseLeftButtonUp">                             
                            <TextBlock Text="{Binding nickname}" Width="120"/>
                            <TextBlock Text="{Binding track}" FontWeight="Bold" Width="120"/>
                            <TextBlock Text="{Binding artist}" Width="120"/>
                        </StackPanel>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </ScrollViewer>

3 个答案:

答案 0 :(得分:1)

您应该使用ListBox中的那个。而不是使用外部ScrollViewer。

假设存在名为“MainListBox”的ListBox,这将使列表顶部的项目每秒前进一次:

var dt = new DispatcherTimer { Interval = TimeSpan.FromSeconds(1) };
dt.Tick += (s, args) =>
                {
                    var count = VisualTreeHelper.GetChildrenCount(this.MainListBox);

                    for (var idx = 0; idx < count; idx++)
                    {
                        var child = VisualTreeHelper.GetChild(this.MainListBox, idx);

                        if (child is ScrollViewer)
                        {
                            var sv = child as ScrollViewer;

                            sv.ScrollToVerticalOffset(sv.VerticalOffset + 1);

                            break;
                        }
                    }
                };
dt.Start();

是的,它可能会更好,但证明它是可能的。

答案 1 :(得分:0)

默认情况下,ListBox有自己的滚动,如果你想将它包装在ScrollViewer中你需要禁用它的滚动:

<ListBox ScrollViewer.VerticalScrollBarVisibility="Disabled"

答案 2 :(得分:0)

ScrollToHorizo​​ntalOffset和ScrollToVerticalOffset在Windows手机开发工具测试版中不起作用,该错误已在beta2版本中修复.... 上面的代码在beta2版本中运行良好。 * *坚持了2天!