ListBox水平滚动方法C#WP 8.0

时间:2014-04-18 08:52:37

标签: c# wpf windows-phone-8 listbox horizontal-scrolling

我正在尝试在我的应用程序中使用输入选择的索引来滚动ListBox(可水平滚动)。我在MSDN和自己的网站上找到了方法ScrollIntoView,但它不起作用,并且在ListBox类页面上,它已被编写为与WP 7.0,7.1兼容。所以,这是我的代码的快照......

scrolling.ScrollIntoView(scrolling.Items[20]);

其中滚动是我的ListBox,第20项是我想要选择和可视化的项目。 PS:我已经尝试过使用selectedIndex方法,但它仍然无效!

这是我的ListBox的一个xaml(放在Layout Grid中),它对App.xaml文档中编写的模板有所了解。

<ListBox x:Name="scrolling"  Grid.Column="0"   ScrollViewer.ManipulationMode ="Control" ScrollViewer.VerticalScrollBarVisibility="Disabled" ScrollViewer.HorizontalScrollBarVisibility="Visible">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal"/>
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
    </ListBox>

编辑:我发现通过按钮调用该函数可以使所有内容工作,但是如何在开始时初始化所有内容?

1 个答案:

答案 0 :(得分:0)

我在我的解决方案中首次使用了更新的UI,然后调用了ScrollIntoView它运行正常:

scrolling.UpdateLayout();
scrolling.ScrollIntoView(scrolling.Items[20]);