ScrollViewer滚动到特定控件

时间:2015-01-31 02:44:38

标签: windows-phone-8 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>

2 个答案:

答案 0 :(得分:0)

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

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

this.scrollViewer2.ScrollToVerticalOffset(0);

答案 1 :(得分:0)

可以通过这种方式计算偏移量。该代码来自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);
}