2014-01-27 105 views
0

我真的与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> 

我不知道为什么它不滚动到所选择的指数?

谢谢!

回答

-1

终于解决了,通过在Lisbox的电网负载父调用lsbReadingChapter.ScrollIntoView。

-1

使用

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

谢谢。正如你可以使用我的代码,但它不起作用。 – ARH

+0

查看更新后的代码 –

+0

我调试过,所选索引已更改,但滚动不起作用。 – ARH

2

使用ScrollIntoView功能或者通过SelectedIndexSelectedItem财产。

lsbReadingChapter.ScrollIntoView(lsbReadingChapter.SelectedIndex); 

lsbReadingChapter.ScrollIntoView(lsbReadingChapter.SelectedItem); 
+0

谢谢。正如你可以使用我的代码,但它不起作用。 – ARH

相关问题