--- title: Optimize control performance description: Windows Presentation Foundation includes many common components that are used in most Windows applications. Learn how to improve user interface performance. ms.date: "03/30/2017" helpviewer_keywords: - "controls [WPF], improving performance" - "container recycling [WPF]" - "user interface virtualization [WPF]" ms.assetid: 45a31c43-ea8a-4546-96c8-0631b9934179 --- # Optimizing performance: Controls Windows Presentation Foundation (WPF) includes many of the common user-interface (UI) components that are used in most Windows applications. This topic contains techniques for improving the performance of your UI. ## Displaying large data sets WPF controls such as the and are used to display lists of items in an application. If the list to display is large, the application's performance can be affected. This is because the standard layout system creates a layout container for each item associated with the list control, and computes its layout size and position. Typically, you do not have to display all the items at the same time; instead you display a subset, and the user scrolls through the list. In this case, it makes sense to use UI *virtualization*, which means the item container generation and associated layout computation for an item is deferred until the item is visible. UI Virtualization is an important aspect of list controls. UI virtualization should not be confused with data virtualization. UI virtualization stores only visible items in memory but in a data-binding scenario stores the entire data structure in memory. In contrast, data virtualization stores only the data items that are visible on the screen in memory. By default, UI virtualization is enabled for the and controls when their list items are bound to data. virtualization can be enabled by setting the attached property to `true`. If you want to enable UI virtualization for custom controls that derive from or existing item controls that use the class, such as , you can set the to and set to `true`. Unfortunately, you can disable UI virtualization for these controls without realizing it. The following is a list of conditions that disable UI virtualization. - Item containers are added directly to the . For example, if an application explicitly adds objects to a , the does not virtualize the objects. - Item containers in the are of different types. For example, a that uses objects cannot implement item recycling because the contains objects of type and . - Setting to `false`. - Setting to `false`. An important consideration when you virtualize item containers is whether you have additional state information associated with an item container that belongs with the item. In this case, you must save the additional state. For example, you might have an item contained in an control and the state is bound to the item's container, and not to the item itself. When the container is reused for a new item, the current value of is used for the new item. In addition, the old item loses the correct value. Currently, no WPF controls offer built-in support for data virtualization. ## Container recycling An optimization to UI virtualization added in the .NET Framework 3.5 SP1 for controls that inherit from is *container recycling,* which can also improve scrolling performance. When an that uses UI virtualization is populated, it creates an item container for each item that scrolls into view and destroys the item container for each item that scrolls out of view. *Container recycling* enables the control to reuse the existing item containers for different data items, so that item containers are not constantly created and destroyed as the user scrolls the . You can choose to enable item recycling by setting the attached property to . Any that supports virtualization can use container recycling. For an example of how to enable container recycling on a , see [Improve the Scrolling Performance of a ListBox](../controls/how-to-improve-the-scrolling-performance-of-a-listbox.md). ## Supporting bidirectional virtualization offers built-in support for UI virtualization in one direction, either horizontally or vertically. If you want to use bidirectional virtualization for your controls, you must implement a custom panel that extends the class. The class exposes virtual methods such as , , , and .These virtual methods enable you to detect a change in the visible part of a list and handle it accordingly. ## Optimizing templates The visual tree contains all the visual elements in an application. In addition to the objects directly created, it also contains objects due to template expansion. For example, when you create a , you also get and objects in the visual tree. If you haven't optimized your control templates, you may be creating a lot of extra unnecessary objects in the visual tree. For more information on the visual tree, see [WPF Graphics Rendering Overview](../graphics-multimedia/wpf-graphics-rendering-overview.md). ## Deferred scrolling By default, when the user drags the thumb on a scrollbar, the content view continuously updates. If scrolling is slow in your control, consider using deferred scrolling. In deferred scrolling, the content is updated only when the user releases the thumb. To implement deferred scrolling, set the property to `true`. is an attached property and can be set on and any control that has a in its control template. ## Controls that implement performance features The following table lists the common controls for displaying data and their support of performance features. See the previous sections for information on how to enable these features. |Control|Virtualization|Container recycling|Deferred scrolling| |-------------|--------------------|-------------------------|------------------------| ||Can be enabled|Can be enabled|Can be enabled| ||Can be enabled|Can be enabled|Can be enabled| ||Not available|Not available|Can be enabled| ||Default|Can be enabled|Can be enabled| ||Default|Can be enabled|Can be enabled| ||Can be enabled|Can be enabled|Can be enabled| ||Not available|Not available|Can be enabled| > [!NOTE] > For an example of how to enable virtualization and container recycling on a , see [Improve the Performance of a TreeView](../controls/how-to-improve-the-performance-of-a-treeview.md). ## See also - [Layout](layout.md) - [Layout and Design](optimizing-performance-layout-and-design.md) - [Data Binding](optimizing-performance-data-binding.md) - [Controls](../controls/index.md) - [Styling and Templating](../controls/styles-templates-overview.md) - [Walkthrough: Caching Application Data in a WPF Application](walkthrough-caching-application-data-in-a-wpf-application.md)