如何滚动到列表框中的选定项目

时间:2014-01-27 12:47:01

标签: c# windows-phone-8

我真的很困惑wp8列表框滚动。下面简单的代码我滚动到选定的索引(项目),但它不起作用。

lsbReadingChapter.SelectionChanged -= lsbReadingChapter_SelectionChanged;
            _lastAyaSelectedIndex = startingAya;
            lsbReadingChapter.ItemsSource = null;
            lsbReadingChapter.ItemsSource = ds.getArabicTextWithTranslation(currentChapter);
            lsbReadingChapter.SelectedIndex = startingAya;
            lsbReadingChapter.UpdateLayout();
            lsbReadingChapter.ScrollIntoView(lsbReadingChapter.SelectedItem);
            lsbReadingChapter.SelectionChanged += lsbReadingChapter_SelectionChanged;

selectedIndex总是大于零,但列表框显示列表中的第一项并且不滚动。

这是我的xaml

ListBox x:Name="lsbReadingChapter" HorizontalAlignment="Stretch" Grid.Row="1" ScrollViewer.VerticalScrollBarVisibility="Visible" Width="480" SelectionChanged="lsbReadingChapter_SelectionChanged" Loaded="lsbReadingChapter_Loaded">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel HorizontalAlignment="Stretch" Width="480" Orientation="Vertical" Background="{Binding Converter={StaticResource AlternateRowConverter}}" >
                        <TextBlock Foreground="Black" Padding="20,0,30,0" TextWrapping="Wrap" HorizontalAlignment="{Binding HAlignment}" FontSize="{Binding ArabicFontSize}">
                            <Run Text="{Binding Aya}"/>
                            <Run Text="{Binding AyaID, StringFormat=﴿\{0\}﴾}" Foreground="Blue" FontSize="30"/>
                        </TextBlock>
                        <TextBlock Padding="20,0,30,0" Text="{Binding AyaTranslation}" Foreground="Black" FontSize="{Binding TranslationFontSize}" TextWrapping="Wrap" HorizontalAlignment="{Binding HAlignment}"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

我不知道为什么它不会滚动到所选索引?

谢谢!

3 个答案:

答案 0 :(得分:2)

ScrollIntoViewSelectedIndex属性使用SelectedItem功能。

lsbReadingChapter.ScrollIntoView(lsbReadingChapter.SelectedIndex);

lsbReadingChapter.ScrollIntoView(lsbReadingChapter.SelectedItem);

答案 1 :(得分:0)

使用

 lsbReadingChapter.ScrollIntoView(lsbReadingChapter.Items[lsbReadingChapter.SelectedIndex]);

答案 2 :(得分:-1)

最终解决了,通过在Lisbox的网格加载父级中调用lsbReadingChapter.ScrollIntoView。