WP7:ListBox什么时候使用VirtualizingStackPanel?
大家都说 ListBox 的默认 ItemsPanel 是 VirtualizingStackPanel。我创建了一个 ListBox 派生类(称为 MyListBox),它默认为 StackPanel。
我的意思是我必须强制虚拟化,例如这样:
const string itemsPanelTemplateString = @"
<ItemsPanelTemplate
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" >
<VirtualizingStackPanel/>
</ItemsPanelTemplate>";
MyListBox {
this.ItemsPanel = (ItemsPanelTemplate)
System.Windows.Markup.XamlReader.Load(itemsPanelTemplateString);
}
我可以在这里重印我的课程,但这不是重点。我想知道一般答案。
该类不会更改预定义的 ListBox 样式,但它使用自己的 ListBoxItem 派生类。
我非常确定使用虚拟化有一些条件,正如我的同事所说,他过去看到过相应的 ListBox 代码。不幸的是,现在我们无法访问 MS dll 的调试版本。
Everybody says the default ItemsPanel for a ListBox is a VirtualizingStackPanel. I created a ListBox-derived class (call it MyListBox) and it defaults to StackPanel instead.
I mean I have to force the virtualization, for example this way:
const string itemsPanelTemplateString = @"
<ItemsPanelTemplate
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" >
<VirtualizingStackPanel/>
</ItemsPanelTemplate>";
MyListBox {
this.ItemsPanel = (ItemsPanelTemplate)
System.Windows.Markup.XamlReader.Load(itemsPanelTemplateString);
}
I could reprint here my class, but that's not the point. I would like to know general answer.
The class does not change predefined ListBox style, but it uses own ListBoxItem-derived class.
I am pretty sure there are some conditions for using virtualization, as my colleague said he saw respective ListBox code in the past. Unfortunately right now we don't have access to the debug versions of MS dll's.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
默认情况下,ListBox 和从 ListBox 派生的控件将 VirtualizedStackPanel 作为 ItemsPanel,除非用户代码明确更改它。
但是,如果您的自定义 ListBox 恰好直接从 ItemsControl 派生(而不是实际从 ListBox 派生),那么您将获得 StackPanel 作为默认 ItemsPanel。
您的代码中可能是这种情况吗?如果没有,请分享您的控制代码。
ListBox and controls derived from ListBox will have VirtualizedStackPanel as the ItemsPanel by default, unless user code changes it explicitely.
However, if your custom ListBox happens to derive directly from ItemsControl (as opossed to actually deriving from ListBox) then you will get StackPanel as the default ItemsPanel.
Could that be the case in your code? If not, please share your control code.
解决了。这是我的错误:
当重写 ListBox.OnApplyTemplate() (出于时间测量的目的)时,我忘记调用 base.OnApplyTemplate()。显然项目面板的选择是在那里完成的。
危险的错误,因为一切似乎都有效。
感谢所有试图提供帮助的人。
Solved. It was my bug:
When overriding ListBox.OnApplyTemplate() (for the purpose of time measurement), I forgot to call base.OnApplyTemplate(). Apparently the selection of the item panel is done there.
Dangerous bug because everything seemingly worked.
Thank you to all who tried to help.
ListBox 的默认样式不分配 ItemsPanel 模板。
根据内部代码,我可以在反射器中看到
OnApplyTemplate
会将VirtualizingStackPanel
分配给内部ItemsHost
如果ItemsPanel
未提供模板。也许包括你的类代码毕竟是一个好主意。
The default style for a ListBox does not assign the ItemsPanel template.
According to the internal code I can see in reflector
OnApplyTemplate
will assign aVirtualizingStackPanel
to the internalItemsHost
if aItemsPanel
template is not supplied.Perhaps including your class code might be a good idea after all.
您可以在 http://blogs.msdn.com/b/slmperf/archive/2010/10/06/silverlight-for-windows-phone-7-listbox-scroll-performance .aspx
这也是一个很好的替代方案,记录在 http://blogs.msdn。 com/b/delay/archive/2010/09/08/never-do-today-what-you-can-put-off-till-tomorrow-deferredloadlistbox-and-stackpanel-help-windows-phone-7-lists- 另一件需要注意的事情(显然)是,
如果您绑定到的集合实现了
IList
,您也只能获得虚拟化。You can find good recommendations on improving performance of the Listbox at http://blogs.msdn.com/b/slmperf/archive/2010/10/06/silverlight-for-windows-phone-7-listbox-scroll-performance.aspx
THer's also a good alternative documented at http://blogs.msdn.com/b/delay/archive/2010/09/08/never-do-today-what-you-can-put-off-till-tomorrow-deferredloadlistbox-and-stackpanel-help-windows-phone-7-lists-scroll-smoothly-and-consistently.aspx
Another thing to note (apparently) is that you'll also only get virtualization if the collection you're binding to implements
IList
.