StackPanel

StackPanel is a very simple panel that arranges its children in a row or a column. You will not normally use StackPanel to lay out your whole user interface. It is most useful for arranging small subsections. Example 3-1 shows how to build a simple search user interface.

Example 3-1. StackPanel search layout

<StackPanel Background="#ECE9D8">
  <TextBlock Margin="3">Look for:</TextBlock>
  <ComboBox  Margin="3"/>
  <TextBlock Margin="3">Filtered by:</TextBlock>
  <ComboBox  Margin="3"/>
  <Button    Margin="3,5">Search</Button>
  <CheckBox  Margin="3">Search in titles only</CheckBox>
  <CheckBox  Margin="3">Match related words</CheckBox>
  <CheckBox  Margin="3">Search in previous results</CheckBox>
  <CheckBox  Margin="3">Highlight search hits (in topics)</CheckBox>
</StackPanel>

Figure 3-1 shows the results. As you can see, the UI elements have simply been stacked vertically one after another. This example used the Margin property to space the elements out a little. Most elements use a single number, indicating a uniform margin all around. The Button uses a pair of numbers to specify different vertical and horizontal margins. This is one of several standard layout properties available on all WPF elements, which are all described in the "Common Layout Properties" section, later in this chapter.

Tip

Many of the examples in this book represent typical snippets of XAML, rather than complete self-contained programs. You can download runnable versions of the examples from the book's web site at http://sellsbrothers.com/writing/wpfbook ...

Get Programming WPF, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.