<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Vincent Sibal's Blog : WPF 3.5 SP1, Item Container Recycling</title><link>http://blogs.msdn.com/vinsibal/archive/tags/WPF+3.5+SP1/Item+Container+Recycling/default.aspx</link><description>Tags: WPF 3.5 SP1, Item Container Recycling</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Recycling that Item Container</title><link>http://blogs.msdn.com/vinsibal/archive/2008/05/14/recycling-that-item-container.aspx</link><pubDate>Thu, 15 May 2008 04:07:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8505793</guid><dc:creator>vinsibal</dc:creator><slash:comments>9</slash:comments><comments>http://blogs.msdn.com/vinsibal/comments/8505793.aspx</comments><wfw:commentRss>http://blogs.msdn.com/vinsibal/commentrss.aspx?PostID=8505793</wfw:commentRss><wfw:comment>http://blogs.msdn.com/vinsibal/rsscomments.aspx?PostID=8505793</wfw:comment><description>&lt;p&gt;Item container recycling is&amp;nbsp;a one of the many new features in SP1.&amp;nbsp; I'm going to talk a little bit about it here.&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;b&gt;What is it?&lt;/b&gt; &lt;br&gt;&lt;/p&gt;


&lt;p class="MsoNormal"&gt;As a little background, the &lt;a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.virtualizingstackpanel.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.windows.controls.virtualizingstackpanel.aspx" target="_blank"&gt;&lt;font color="#0000ff"&gt;VirtualizingStackPanel&lt;/font&gt;&lt;/a&gt;’s virtualization scheme basically works like this: generate containers when needed and throw them away when they are no longer in view.&amp;nbsp; This does solve the memory issue that is created when the panel has a large number of items, but it can still be very expensive to have to recreate and throw away containers every time they go out of view.&amp;nbsp; Enter container recycling.&amp;nbsp; Container recycling is a performance optimization to this virtualization scheme.&amp;nbsp; It basically reuses existing containers so they do not have to be&amp;nbsp;recreated each time they&amp;nbsp;come back into view.&lt;br&gt;&amp;nbsp;&lt;br&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;b&gt;How do I use it?&lt;/b&gt;&lt;br&gt;&amp;nbsp;&lt;br&gt;VirtualizingStackPanel’s virtualization will still work the same as before.&amp;nbsp; In fact container recycling will be turned off by default.&amp;nbsp; I will explain how to turn it on shortly, but before that let me describe the new API.&amp;nbsp; VSP (VirtualizingStackPanel) has added a new attached property, VirtualizationModeProperty, as well as the getter and setter for it, GetVirtualizationMode and SetVirtualizationMode.&amp;nbsp; This property is used to describe the type of mode that it can be in, VirtualizationMode.Standard or VirtualizationMode.Recycling.&amp;nbsp; The former will use the standard virtualization scheme just as it normally would and the latter will include recycling to the virtualization scheme.&amp;nbsp; The VirtualizationModeProperty is to be set from the ItemsControl that hosts the items presented by the VSP.&amp;nbsp; By default, it is set to VirtualizationMode.Standard.&lt;/p&gt;
&lt;p class="MsoNormal"&gt;Here is an example of turning on recycling on a &lt;a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.listbox.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.windows.controls.listbox.aspx" target="_blank"&gt;&lt;font color="#0000ff"&gt;ListBox&lt;/font&gt;&lt;/a&gt;:&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; color: blue; font-family: 'Courier New';"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size: 10pt; color: rgb(163, 21, 21); font-family: 'Courier New';"&gt;ListBox&lt;/span&gt;&lt;span style="font-size: 10pt; color: red; font-family: 'Courier New';"&gt; VirtualizingStackPanel.VirtualizationMode&lt;/span&gt;&lt;span style="font-size: 10pt; color: blue; font-family: 'Courier New';"&gt;="Recycling" …/&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;Note that ListBox uses a VSP as its &lt;a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.itemspaneltemplate.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.windows.controls.itemspaneltemplate.aspx" target="_blank"&gt;&lt;font color="#0000ff"&gt;ItemsPanel&lt;/font&gt;&lt;/a&gt; and sets the &lt;a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.virtualizingstackpanel.isvirtualizing.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.windows.controls.virtualizingstackpanel.isvirtualizing.aspx" target="_blank"&gt;&lt;font color="#0000ff"&gt;VirtualizingStackPanel.IsVirtualizing&lt;/font&gt;&lt;/a&gt; attached property to true by default.&amp;nbsp; If you were using &lt;a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.aspx" target="_blank"&gt;&lt;font color="#0000ff"&gt;ItemsControl&lt;/font&gt;&lt;/a&gt; directly or creating a custom ItemsControl that uses a VSP as its ItemsPanel, you will need to set both attached properties to get the recycling behavior.&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; color: blue; font-family: 'Courier New';"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size: 10pt; color: rgb(163, 21, 21); font-family: 'Courier New';"&gt;ItemsControl&lt;/span&gt;&lt;span style="font-size: 10pt; color: red; font-family: 'Courier New';"&gt; VirtualizingStackPanel.IsVirtualizing&lt;/span&gt;&lt;span style="font-size: 10pt; color: blue; font-family: 'Courier New';"&gt;="true"&lt;/span&gt;&lt;span style="font-size: 10pt; color: red; font-family: 'Courier New';"&gt; VirtualizingStackPanel.VirtualizationMode&lt;/span&gt;&lt;span style="font-size: 10pt; color: blue; font-family: 'Courier New';"&gt;="Recycling" …/&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;br&gt;&lt;b&gt;Special considerations&lt;/b&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;Just as with the virtualization scheme, marking containers as non-virtualizable will not virtualize or recycle the container.&amp;nbsp; You can do this by handling VSP’s &lt;a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.virtualizingstackpanel.cleanupvirtualizeditem.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.windows.controls.virtualizingstackpanel.cleanupvirtualizeditem.aspx" target="_blank"&gt;&lt;font color="#0000ff"&gt;CleanUpVirtualizedItem&lt;/font&gt;&lt;/a&gt; event.&amp;nbsp; This event is fired when a container is about to be thrown away or recycled.&amp;nbsp; You have the option here of cancelling the operation.&amp;nbsp; &lt;/p&gt;
&lt;p class="MsoNormal"&gt;Some other helpful methods for you to have more control of the data items and containers include ItemsControl.&lt;a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.preparecontainerforitemoverride.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.preparecontainerforitemoverride.aspx" target="_blank"&gt;&lt;font color="#0000ff"&gt;PrepareContainerForItemOverride&lt;/font&gt;&lt;/a&gt; which is called before a container is used and ItemsControl.&lt;a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.clearcontainerforitemoverride.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.clearcontainerforitemoverride.aspx" target="_blank"&gt;&lt;font color="#0000ff"&gt;ClearContainerForItemOverride&lt;/font&gt;&lt;/a&gt; which is called when a container is thrown away or recycled. &lt;br&gt;&lt;/p&gt;

&lt;p class="MsoNormal" mce_keep="true"&gt;For more information regarding scrolling performance take a look at this &lt;a href="http://download.microsoft.com/download/2/d/b/2db72dcf-5ae0-445f-b709-7f34437d8b21/Scrolling_in_WPF.doc" target="_blank" mce_href="http://download.microsoft.com/download/2/d/b/2db72dcf-5ae0-445f-b709-7f34437d8b21/Scrolling_in_WPF.doc"&gt;article&lt;/a&gt; by one our dev's here on the WPF team.&amp;nbsp; It came out before this feature so the information regarding container recycling is a suggestion on how to implement it and not how to actually use it.&amp;nbsp; It still has a lot of great information though.&lt;br&gt;&lt;/p&gt;&lt;p class="MsoNormal" mce_keep="true"&gt;&amp;nbsp;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8505793" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/vinsibal/archive/tags/Item+Container+Recycling/default.aspx">Item Container Recycling</category><category domain="http://blogs.msdn.com/vinsibal/archive/tags/WPF+3.5+SP1/default.aspx">WPF 3.5 SP1</category></item></channel></rss>