Placing controls in a Stack
Another simple and useful layout panel in WPF is a StackPanel
. It works almost like a WrapPanel
, but with a difference that it can't wrap the child elements to a new line. All items added inside it either get placed in horizontal or vertical stacks.
Note
The StackPanel
measures its children using either native or relative sizing, keeping the arrangement pass simple by laying out the items in order.
However, the Grid
uses complex combinations of child elements when proportional sizing or auto sizing is used. Thus, it makes the Grid
layout have a slow to medium performance for the measure pass and the arrangement pass to execute.
Therefore, wherever possible, the StackPanel
preferable to over the Grid
panel to reduce the rendering overhead.
In this recipe, we will learn how the StackPanel
works, by using a very simple example.
Getting ready
To get started, let's open Visual Studio and create a new WPF application project named CH03.StackPanelDemo
.
How to do it...
Inside...