Skip to content

UI Virtualization in WPF

Let’s talk about improving performance using VirtualizingStackPanel, when displaying large sets of grouped data , The VirtualizingStackPanel stacks elements vertically or horizontally and leverages virtualization to help reduce the number of UI elements created in your screen[window] especially while working with controls like Combo box, List box , List View , Tree View etc.  UI elements are generated from a larger number of data based on which items are visible on the screen, if we are loading a huge data in the control there may be a performance issue during loading and navigating. It is intensive both in terms of memory and processor to generate a large number of UI elements when only few are visible on the screen at a given time.

A control that inherits VirtualizingPanel, such as the VirtualizingStackPanel, calculates visible items and works with the ItemContainerGenerator from an ItemsControl to only create UI elements for visible items, the VirtualzingStackPanel can determine which items in the list are currently visible on screen, and generates only the UI elements needed for those items that can help you increase the performance of your application.

Assembly:  PresentationFramework.dll             

 Namespace:  System.Windows.Controls

Following xaml code snippet shows an example of implementing

<ListBox Name=”ListBoxVirtualization” Height=”110″ Margin=”5″


VirtualizingStackPanel.VirtualizationMode=”Recycling” />

By default VirtualizingStackPanel is implemented on listBox. You can manually enable or disable virtualization on listbox control by setting VirtualizingStackPanel.IsVirtualizing property to “True” or “False”. Internally VirtualizingStackPanel creates item container for each visible item. When item is not visible it destroys that item container. So some times with huge data it is very expensive task to create and destroy item container for each visible item.

In dotnet framework 4.0 and above Microsoft introduced VirtualizationMode. If you set VirtualizingStackPanel.VirtualizationMode = “Recycling” then each container will get reuse instead of destroy. You will get better performance by specifying VirtualizationMode to Recycling than basic default virtualization. If you don’t specify VirtualizationMode then it will use default basic virtualization.

Let’s assume in our case study we have listbox control with 1 million items and we can see only 10 items at a time. So the only 10 items which are currently visible will get created and maintained by the listbox.  Instead of loading 1 million items at a time on listbox control it loads only 10 items which are visible.

SQL Server query to create directory in the network system

Code Snippet:


USE Master


— Create Directory in the Local Machine

— Declare Variables

DECLARE @DBName sysname

DECLARE @NetworkPath nvarchar(200)

DECLARE @DirTree TABLE (subdirectory nvarchar(250), depth INT)

— Initialize variables

SET @DBName = ‘CreateDirectory’

SET @NetworkPath = ‘\\Network Path\’ + @DBName

— @NetworkPath values

INSERT INTO @DirTree(subdirectory, depth)

EXEC master.sys.xp_dirtree @NetworkPath

— Create the @NetworkPath directory

IF NOT EXISTS (SELECT 1 FROM @DirTree WHERE subdirectory = @DBName)

EXEC master.dbo.xp_create_subdir @NetworkPath


PRINT ‘Successfully created new directory :)’