【问题标题】:How to Scroll into selected item in listbox如何滚动到列表框中的选定项目
【发布时间】:2014-01-27 12:47:01
【问题描述】:

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

我不知道为什么它不滚动到选定的索引?

谢谢!

【问题讨论】:

    标签: c# windows-phone-8


    【解决方案1】:

    通过SelectedIndexSelectedItem 属性使用ScrollIntoView 函数。

    lsbReadingChapter.ScrollIntoView(lsbReadingChapter.SelectedIndex);
    

    lsbReadingChapter.ScrollIntoView(lsbReadingChapter.SelectedItem);
    

    【讨论】:

      【解决方案2】:

      使用

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

      【讨论】:

      • 我调试了一下,选择的索引变了,但是滚动不起作用。
      【解决方案3】:
      public static void ScrollToSelectedItem(ListBox control)
      {
          if (control.SelectedIndex != -1)
              control.TopIndex = control.SelectedIndex;
      }
      

      【讨论】:

      • 请记住,Stack Overflow 不仅仅是为了解决眼前的问题,而是为了帮助未来的读者找到类似问题的解决方案,这需要了解底层代码。这对于我们社区的初学者和不熟悉语法的成员来说尤其重要。鉴于此,您能否edit 您的答案包括对您正在做什么的解释以及为什么您认为这是最好的方法?
      【解决方案4】:

      终于解决了,在Lisbox的grid load parent中调用lsbReadingChapter.ScrollIntoView。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多