2014-01-27 106 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

相關問題