Windows phone 8 ScrollViewer滚动到特定控件

Windows phone 8 ScrollViewer滚动到特定控件,windows-phone-8,scrollviewer,Windows Phone 8,Scrollviewer,我有一个scrollviewer,上面有一个项目模板。用户发送一个控制编号,我需要滚动到该项目。如何找到scrollviewer的偏移量 这是我正在使用的scrollviewer <ScrollViewer Grid.Row="0" x:Name="ScrollPanel" Padding="0"> <Grid x:Name="TestPanel" > <ItemsControl x:Name="MainItemsControl" Items

我有一个scrollviewer,上面有一个项目模板。用户发送一个控制编号,我需要滚动到该项目。如何找到scrollviewer的偏移量

这是我正在使用的scrollviewer

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

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

您没有属性,但有两种方法:ScrollToVerticalOffset垂直滚动和ScrollToHorizontalOffset水平滚动

this.scrollViewer2.ScrollToVerticalOffset(0);

可以用这种方法计算偏移量。该守则是从英国引进的


哪个版本的Windows Phone?这很重要,所以你应该提到-使用正确的标签。谢谢,它的windows phone 8I正在尝试这种技术,但是itemContainer返回null。虽然项目返回正确。好的,我让它工作,但我必须在第二行之前添加UpdateLayout使其工作。如果我使用var itemContainer=mainitemscoontrol.ItemContainerGenerator.ContainerFromIndexindex,它也可以工作;与第一行不同的是,我留下的唯一问题是,页面上的其他内容仍在更新,随着页面的运行,上面的内容变得越来越长,我想要查看的项目被推到了底部。我确信我只需要确保在加载所有内容后运行它。@PCur很高兴您成功了。我将根据您的反馈调试代码,并在我使用计算机时更新我的答案。您好@PCur,我已更新了我的答案。ContainerFromIndex更好,因为提供了索引,另一个修改是将ContainerFromIndex的返回值转换为UIElement而不是FrameworkElement。谢谢!它工作得很好。正如您在我的原始帖子中看到的,我使用一个名为BBCode的本地属性来设置RichTextBox。它基于从网站返回的bbcode,并设置字体、图片等。您知道在加载上面的内容时,是否有方法保持控件在视图中?
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);
}