concept StackPanel in category silverlight

appears as: StackPel, StackPanel, The StackPel, StackPel
Silverlight 5 in Action

This is an excerpt from Manning's book Silverlight 5 in Action.

Certain types of objects may contain one or more other nested objects. For example, a button may contain a single content object, which itself may contain one or more other objects. In listing 2.1, the UserControl contains the Grid, the Grid contains the Button, and the Button contains a StackPanel, which is a panel that by default lays its children out in a vertical list. The StackPanel itself contains three TextBlock elements.

Listing 2.1. XAML showing a hierarchy of nested objects

The UserControl and Button are both content controls, a concept I’ll discuss in greater detail in chapter 12. For now, it’s important to understand that a content control may only have one direct child element, typically a panel (StackPanel, Grid, Canvas, etc.) that holds other elements. The x:Name and x:Class properties are part of the namespace specified by the xmlns:x statement. More on that in a moment ... The Grid and StackPanel are both Panels, which is a type that has a Children collection to allow multiple contained elements. I’ll discuss panels in detail in chapter 8.

Listing 7.2. Two visible elements and one collapsed element in a StackPanel

Though numerous types of panels are available, the four most important and widely used are Canvas, StackPanel, WrapPanel, and Grid. Of them, Grid, the most widely used and most flexible of the panels, and also the default for UserControl and Page templates, is arguably the most important panel to understand.

I’ll start with the simplest panel, Canvas, then move on to panels that provide more layout functionality. The StackPanel forms the basis of most list and menu implementations but is still relatively simple in its layout and functionality. WrapPanel is similar to StackPanel but provides automatic wrapping of its child elements. Grid, the final panel in this section, is typically the root of your interfaces and is one of the most powerful, flexible, and complex panels available.

The StackPanel is a panel that arranges items either vertically or horizontally one after the other. Elements in a StackPanel are given as much room as they need, and no more. If you consider a ListBox, for example, the elements are arranged in a vertical list, with each element having enough room to display itself (which can differ from element to element in the same StackPanel); the elements don’t overlap.

By default, the ListBox uses a StackPanel control for the arrangement of those items. Many other controls benefit from this simple layout technique, especially various types of menus and navigation elements.

Listing 8.3 shows a simple StackPanel with three items positioned vertically.

Silverlight 4 in Action: Silverlight 4, MVVM, and WCF RIA Services

This is an excerpt from Manning's book Silverlight 4 in Action: Silverlight 4, MVVM, and WCF RIA Services.

Certain types of objects may contain one or more of other nested objects. For example, a button may contain a single content object, which itself may contain one or more other objects. In listing 2.1, the UserControl contains the Grid, the Grid contains the Button, and the Button contains a StackPanel, which is a panel that by default lays its children out in a vertical list. The StackPanel itself contains three TextBlock elements.

Much like a word is a grouping of letters in a crossword puzzle, a StackPanel is a grouping of visual elements. Each successive visual element is positioned vertically or horizontally within a single row or column, as seen in listing 7.3.

Nesting layout panels is incredibly important when you begin to consider the entire scope of an application. Although the StackPanel is great for one-dimensional (vertical or horizontal) content, it’s not suited for organizing large amounts of elements. Consider the illustration in figure 7.2.

Figure 7.2. A basic purchase order, using tabular layout. This would be perfect for a Grid.

Imagine attempting to recreate the purchase order shown in figure 7.2 using a series of StackPanel elements. Up front, you’d have to decide if you want to create vertical or horizontal elements. Then, you’d have to specify the Width of each StackPanel because StackPanel elements are arranged and sized independently of each other. There has to be a better way to organize tabular data. Thankfully, Silverlight provides the powerful Grid panel to do just that.