Automatically repositioning controls using WrapPanel
WrapPanel
in WPF is similar to StackPanel
, but it does not stack the items in a single line; rather it wraps the items to a new line based on the available space. It also looks like a UniformGrid
control, but it has odd cell size depending on the item dimension.
In this recipe, we will cover more about WrapPanel
and how to reposition controls using it.
Getting ready
To get started, open Visual Studio IDE and create a new project named CH03.WrapPanelDemo
. Make sure to select the WPF app template while creating the project.
How to do it...
Let's look at a simple example to add a few buttons in WrapPanel
. Perform the following steps to design the UI:
- From the Visual Studio
Solution Explorer
, open theMainWindow.xaml
page. - Replace the existing
Grid
panel with aWrapPanel
control and set itsOrientation
property toHorizontal
. - Add a few button controls of diverse sizes. The entire XAML inside the window will look like the following code:
<WrapPanel...