There are many times when more than one panel would suitable for the layout I want, however I know there is a difference in render times for different panel types.
For example, MSDN states that
A relatively simple
Panel
, such asCanvas
, can have significantly
better performance than a more complexPanel
, such asGrid
.
So in terms of render time and performance, in what order are WPF panels the most efficient?
WPF Panels:
Canvas
DockPanel
Grid
UniformGrid
StackPanel
WrapPanel
VirtualizingPanel
/VirtualizingStackPanel
I'm fairly sure I saw a list of this somewhere online, but I can't find it now.
The ideal answer I am looking for would provide me a list of panels in the order that they would render fastest. I understand the number of children is a big factor in how efficient the panels are, so for the sake of this question, assume each panel only has a Label
/TextBox
pair.
In addition, I would like a list of exceptions, such as specific panels which perform better than others based on certain conditions.
Update
To summarize based on the accepted answer below, the panel performance is based on the number and layout of child items, however in general the list from fastest to slowest is:
Canvas
StackPanel
WrapPanel
DockPanel
Grid
In addition, a VirtualizingPanel
/ VirtualizingStackPanel
should always be used if there are a lot of items that don't always fit on the screen.
I'd highly recommend you read the accepted answer below for more details before just picking an item from this list.
Best Answer
I think it is more concise and understandable to describe the performance characteristics of each panel than it is to try to give an absolute relative performance comparison.
WPF makes two passes when rendering content: Measure and Arrange. Each panel has different performance characteristics for each of these two passes.
The performance of the measure pass is most affected by the ability of a panel to accommodate stretching using alignments (or Auto in the case of the
Grid
) and then the number of children which are stretched or auto-sized. The performance of the Arrange pass is affected by the complexity of the interaction between layout location of different children and then of course the number of children.At times the given panels don't easily lend themselves to the needed layout. I created a control that needed an arbitrary number of items to each be positioned at a certain percentage of the available space. None of the default controls do this. Attempting to make them do this (via binding to the actual size of the parent) results in horrible performance. I created a layout panel based on the Canvas which achieved my desired result with minimal work (I copied the source for the canvas and modified around 20 lines of it).
Available Panels:
Canvas
The Canvas has the best performance of all the panels for the arrange pass since each item is statically assigned a location. The measure pass also has excellent performance since there is no concept of stretching in this panel; each child simply uses its native size.
DockPanel
The Dockpanel has a very simple layout scheme where items are added one by one relative to the previous item added. By default either the height or width is determined by the item's native size (based on Top/Bottom vs Left/Right respectively) and the other direction is determined by the
Dock
property if the width or height is undefined. Medium to fast measure pass and medium to fast arrangement pass.Grid
This can be the most performance intensive panel if proportional sizing or auto sizing is used. Calculating child item size can be a complex combination of the native size of the item and the layout specified by the grid. Layout is also the most complicated of all the panels. Slow to medium performance for the measure pass and slow to medium performance for the arrangement pass.
StackPanel
The StackPanel measures its children using either native or relative sizing in the opposite direction from its orientation and native sizing in the direction of its orientation (alignment does nothing in this direction). This makes it a mid-level performer in this area. The Arrangement pass is simply, just laying out the items in order. Probably the second-best performance for this pass. Medium performance for the measure pass and fast performance for the layout pass.
VirtualizingPanel
A base class for implementing your own virtualizing panel. Only loads visible items to prevent unneeded use of memory and processor. MUCH more performant for sets of items. Probably slightly less performant for items that fit on the screen due to the bounds checking. The SDK only provides one subclass of this, the
VirtualizingStackPanel
.WrapPanel
The measure pass is a somewhat complex pass where the largest item for a particular row determines the height of the row and then each item on that row either uses its native height (if it has one) or the height of the row. The layout pass is simple, putting each item one after the other on a row and then continuing onto the next row when there is not enough room for the next item. Medium performance measure pass. Medium to fast performance for the arrangement pass.
References:
From Optimizing Performance: Layout and Design
From Measuring and Arranging Children