Chapter 6. Panels
This chapter covers
- The Panel, Canvas, and Grid
- The StackPanel and VirtualizingStackPanel
- Creating a panel from scratch
In the previous chapter you learned about the WinRT XAML layout system. This system is based on the UIElement class, the FrameworkElement class, and, most importantly, the Panel base class.
This chapter is about the classes that derive from the Panel base class. Collectively, these are referred to simply as panels. A panel in XAML contains any number of child elements in the Children collection. How it arranges those children is up to the panel itself. Some panels use simple X,Y positioning, some use a row and column approach, and some simply automatically arrange based on the order in which elements are added.
Panels are vital to working in XAML. Before you place your first Button, or TextBlock, or other control, you’ll start with a panel. A solid understanding of how each panel is intended to be used will save you a great deal of debugging later. I can’t tell you how many times I’ve seen developers struggling with a layout in nested panels where some element just won’t position itself properly. In just about every case, it has been because the developer tried to force a panel to do something another panel was better suited for. Luckily, because panels all have a common API for adding children, swapping out one panel for another is relatively easy.