[英]How to Scroll into selected item in listbox
我真的對 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>
我不知道為什么它不滾動到選定的索引?
謝謝!
通過SelectedIndex
或SelectedItem
屬性使用ScrollIntoView
函數。
lsbReadingChapter.ScrollIntoView(lsbReadingChapter.SelectedIndex);
要么
lsbReadingChapter.ScrollIntoView(lsbReadingChapter.SelectedItem);
public static void ScrollToSelectedItem(ListBox control)
{
if (control.SelectedIndex != -1)
control.TopIndex = control.SelectedIndex;
}
采用
lsbReadingChapter.ScrollIntoView(lsbReadingChapter.Items[lsbReadingChapter.SelectedIndex]);
終於解決了,方法是在Lisbox的父級網格加載中調用lsbReadingChapter.ScrollIntoView。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.