Windows Phone 7 ListBox 滚动到最后一条的问题 ( ListBox scrolling to the bottom )

最近做一个windows phone 7 聊天软件的项目,聊天列表用的是Listbox,DataTemplate用了十几种模板,而且其中的有的模板高度是随着聊天的内容长度变化的,这就导致了,ListBox 滚动时出现了很多奇怪的现象:滚动不到最后一条,卡在某一条不能滚动,屏幕晃动等。从网上找了很多关于ListBox的内容,这条最有帮助:  ListBox performance,是微软的开发人员讲述的ListBox使用规范,这其中重点强调了要使ListBox中的DataTemplate要有固定高度。但这无法满足我的DataTemplate对变高的需求。在查看中,发现ListBox 默认用到了虚拟化VirtualizingStackPanel (VSP),试着去掉这个虚拟化改用stackpanel后,发现问题一下解决了。

下面是代码,加到ListBox 中就好了。

  <ListBox.ItemsPanel>
             <ItemsPanelTemplate>
                       <StackPanel>
                         </StackPanel>
             </ItemsPanelTemplate>
   </ListBox.ItemsPanel>

  VirtualizingStackPanel (VSP)虚拟化虽然能提高显示性能,但是在变高DataTemplate情况下效果很差,去除后,滚动到ListBox底部变得很美观,最后一项也不会被遮挡半边了。

当然要想让ListBox滚动到底部,还是比较容易的,直接调用ListBox自带的Api就好了:

this.ListBox.UpdateLayout();
if (this.ListBox.Items.Count > 0)
this.ListBox.ScrollIntoView(this.ListBox.Items.ElementAt(this.ListBox.Items.Count - 1));
this.ListBox.UpdateLayout();

 

posted @ 2012-06-27 17:50  月食之后  阅读(507)  评论(0编辑  收藏  举报