Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There's actually much more to it than just making the <code>ItemsPanelTemplate</code> use <code>VirtualizingStackPanel</code>. The default <code>ControlTemplate</code> for <code>ItemsControl</code> does not have a <code>ScrollViewer</code>, which is the key to virtualization. Adding to the the default control template for <code>ItemsControl</code> (using the control template for <code>ListBox</code> as a template) gives us the following: </p> <pre><code>&lt;ItemsControl VirtualizingStackPanel.IsVirtualizing="True" ScrollViewer.CanContentScroll="True" ItemsSource="{Binding Path=AccountViews.Tables[0]}"&gt; &lt;ItemsControl.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;TextBlock Initialized="TextBlock_Initialized" Text="{Binding Path=Name}" /&gt; &lt;/DataTemplate&gt; &lt;/ItemsControl.ItemTemplate&gt; &lt;ItemsControl.ItemsPanel&gt; &lt;ItemsPanelTemplate&gt; &lt;VirtualizingStackPanel /&gt; &lt;/ItemsPanelTemplate&gt; &lt;/ItemsControl.ItemsPanel&gt; &lt;ItemsControl.Template&gt; &lt;ControlTemplate&gt; &lt;Border BorderThickness="{TemplateBinding Border.BorderThickness}" Padding="{TemplateBinding Control.Padding}" BorderBrush="{TemplateBinding Border.BorderBrush}" Background="{TemplateBinding Panel.Background}" SnapsToDevicePixels="True"&gt; &lt;ScrollViewer Padding="{TemplateBinding Control.Padding}" Focusable="False"&gt; &lt;ItemsPresenter SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" /&gt; &lt;/ScrollViewer&gt; &lt;/Border&gt; &lt;/ControlTemplate&gt; &lt;/ItemsControl.Template&gt; &lt;/ItemsControl&gt; </code></pre> <p>(BTW, a great tool for looking at default control templates is <a href="http://www.sellsbrothers.com/Posts/Details/2091" rel="noreferrer">Show Me The Template</a>)</p> <p>Things to notice:</p> <p>You have to set <code>ScrollViewer.CanContentScroll="True"</code>, see <a href="https://stackoverflow.com/questions/1924089/listbox-virtualizingstackpanel-and-smooth-scrolling-in-wpf">here</a> for why.</p> <p>Also notice that I put <code>VirtualizingStackPanel.VirtualizationMode="Recycling"</code>. This will reduce the numbers of times <code>TextBlock_Initialized</code> is called to however many TextBlocks are visible on the screen. You can read more on UI virtualization <a href="https://web.archive.org/web/20150708100804/http://www.zagstudio.com:80/blog/497" rel="noreferrer">here </a>.</p> <p>EDIT: Forgot to state the obvious: as an alternate solution, you can just replace <code>ItemsControl</code> with <code>ListBox</code> :) Also, check out this <a href="https://msdn.microsoft.com/en-us/library/vstudio/cc716879(v=vs.100).aspx" rel="noreferrer">Optimizing Performance on MSDN page</a> and notice that <code>ItemsControl</code> isn't in the "Controls That Implement Performance Features" table, which is why we need to edit the control template.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload