【问题标题】:ScrollViewer scroll to specific controlScrollViewer 滚动到特定控件
【发布时间】:2015-02-01 21:22:56
【问题描述】:

我有一个带有项目模板的滚动查看器。用户发送一个控制号,我需要滚动到该项目。如何找到滚动查看器的偏移量?

这是我正在使用的滚动查看器

<ScrollViewer Grid.Row="0" x:Name="ScrollPanel" Padding="0">
    <Grid x:Name="TestPanel" >
        <ItemsControl x:Name="MainItemsControl" ItemsSource="{Binding PostList}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <StackPanel x:Name="PostPanel" Margin="0,5,0,5">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="60" />
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="Auto" />
                            </Grid.ColumnDefinitions>
                            <Image x:Name="IconUrl" Source="{Binding IconUrl}" Grid.Row="0" Grid.Column="0" Margin="12,0,12,0" VerticalAlignment="Center" HorizontalAlignment="Center" Width="50"/>
                            <StackPanel Grid.Row="0" Grid.Column="1">
                                <TextBlock x:Name="PostAuthorName" Text="{Binding PostAuthorName}" TextWrapping="NoWrap" Margin="12,0,12,0" Foreground="Black" Style="{StaticResource PhoneTextNormalStyle}"/>
                                <TextBlock x:Name="PostTime" Text="{Binding PostTime}" TextWrapping="NoWrap" Margin="12,0,12,0" Foreground="Black" Style="{StaticResource PhoneTextSubtleStyle}"/>
                            </StackPanel>
                            <TextBlock x:Name="ReplyNumber" Text="{Binding ReplyNumber}" Grid.Row="0" Grid.Column="2" TextWrapping="NoWrap" Foreground="Black" Margin="12,0,12,0" Style="{StaticResource PhoneTextNormalStyle}" VerticalAlignment="Center"/>
                        </Grid>
                        <RichTextBox x:Name="PostContent" localcontrols:Properties.BBCode="{Binding PostContent}" />
                        <Rectangle Grid.Row="1"  Fill="Gray" Height="1" HorizontalAlignment="Stretch" Margin="0,10,0,10"/>
                    </StackPanel>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>
</ScrollViewer>

【问题讨论】:

  • 哪个版本的 Windows Phone?这很重要,所以你应该用正确的标签提到这一点。
  • 谢谢,它的 windows phone 8

标签: windows-phone-8 scrollviewer


【解决方案1】:

可以通过这种方式计算偏移量。代码取自this answer。

void ScrollToIndex(int index)
{
    var itemContainer = MainItemsControl.ItemContainerGenerator.ContainerFromIndex(index) as UIElement;
    GeneralTransform transform = itemContainer.TransformToVisual(ScrollPanel);
    Point position = transform.Transform(new Point(0, 0));
    ScrollPanel.ScrollToVerticalOffset(position.Y);
}

【讨论】:

  • 我正在尝试这种技术,但是我为 itemContainer 返回了 null。项目确实返回正确。
  • 好的,我得到了这个工作,但我必须在第二行之前添加 UpdateLayout() 才能使其工作。如果我做了 var itemContainer = MainItemsControl.ItemContainerGenerator.ContainerFromIndex(index); 它也有效而不是第一行我留下的唯一问题是页面上的其他内容仍在更新,并且由于它正在运行,因此上面的内容变得更长,并且我想要查看的项目被推到了底部。我确信我只需要确保它在加载完所有内容后运行。
  • @PCur 很高兴你成功了。我将根据您的反馈调试代码,并在我使用计算机时更新我的​​答案。
  • 嗨@PCur,我已经更新了我的答案。 ContainerFromIndex 更好,因为提供了索引,另外一个修改是 ContainerFromIndex 的返回值被强制转换为 UIElement 而不是 FrameworkElement。
  • 谢谢!效果很好。从我的原始帖子中可以看出,我使用一个名为 BBCode 的本地属性来设置 RichTextBox。它基于从网站返回的 bbcode 并设置字体、图片等。你知道是否有办法在加载上面的内容时将控件保持在视图中?
【解决方案2】:

ItemsControl 没有实现允许滚动到特定项目的逻辑。您可以改用 ListBox 控件。此控件有一个 ScrollIntoView 方法,该方法接受您希望滚动到的对象作为参数。您仍然可以通过设置 ListBox 控件的 ItemsPanel 属性来使用 RadWrapPanel。

您没有属性,但您有两种方法:ScrollToVerticalOffset(垂直滚动)和 ScrollToHorizo​​ntalOffset(水平滚动)。

this.scrollViewer2.ScrollToVerticalOffset(0);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-02
    相关资源
    最近更新 更多