Layout Elements

Summary

Panel and Panel-Derived Elements

Panel class is the abstract base class for all of the Panel-derived classes below (Canvas, Grid, StackPanel, DockPabel, TabPanel, WrapPanel, etc.) A Panel-derived class is used to position and arrange child elements. Note that Panel-derived elements do not receive focus or stylus events if the Background property is not set. To compel a panel element to receive focus, set the Focusable property to true.

The following common members are of critical importance in using and understanding WPF layout:

Width, Height and Alignment

The following screens shows how Height, Width, HorizontalAlignment, and VerticalAlignment interact:

If Height or Width is not supplied, then ‘Auto’ is assumed to be the default value. Auto sizing behaviour implies that the element will fill the space available to it (note that specific controls frequently supply default values through their default theme styles that will disable the auto sizing behaviour unless it is specifically re-enabled):

 The following XAML is equivalent to the above:

Setting Height and Width explicitly (note that VerticalAlignment and HorizontalAlignment both default to Center):

Same as above except that VerticalAlignment is explicitly set to Top:

 

Same as above except that HorizontalAlignment is explicitly set to Left:

If no Width or Height are specified, then output becomes invisible: 

Attached Properties

Panel-derived elements make extensive use of attached properties as shown below. See first example for Canvas.

Panel-Derived Classes

There are six defined panel classes (Canvas, Grid, DockPanel, StackPanel, VirtualizingStackPanel, and WrapPanel) that are designed specifically for creating application UI. Each panel element encapsulates its own special functionality, as seen in the following table.

Element Name

UI Panel?

Description

Canvas

Yes

Defines an area within which you can explicitly position child elements by coordinates relative to the Canvas area.

DockPanel

Yes

Defines an area within which you can arrange child elements either horizontally or vertically, relative to each other.

Grid

Yes

Defines a flexible grid area consisting of columns and rows. Child elements of a Grid can be positioned precisely using the Margin property.

StackPanel

Yes

Arranges child elements into a single line that can be oriented horizontally or vertically.

TabPanel

No

Handles the layout of tab buttons in a TabControl.

ToolBarOverflowPanel

No

Arranges content within a ToolBar control.

UniformGrid

No

UniformGrid is used to arrange children in a grid with all equal cell sizes.

VirtualizingPanel

No

Provides a base class for panels that can "virtualize" their children collection.

VirtualizingStackPanel

Yes

Arranges and virtualizes content on a single line oriented horizontally or vertically.

WrapPanel

Yes

WrapPanel positions child elements in sequential position from left to right, breaking content to the next line at the edge of the containing box. Subsequent ordering happens sequentially from top to bottom or right to left, depending on the value of the Orientation property.

Canvas

A Canvas is a layout control that enables absolute positioning of child elements using coordinates that are relative to the canvas area. Note the following:

Example 1

This example shows the basic usage of Canvas. Note how child elements of Canvas use the Top and Left Canvas attached-properties:

<Page WindowTitle="Canvas Sample" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
  <Canvas Background="LightSteelBlue">

        <TextBlock Canvas.Top="10" Canvas.Left="10" FontSize="14">Hello World!</TextBlock>
        <TextBlock Canvas.Top="30" Canvas.Left="75" FontSize="20" >Isn't absolute positioning handy?</TextBlock>
        <Button    Canvas.Top="70" Canvas.Left="50" Content="Click Me" Width="100"></Button>

  </Canvas>
</Page>

Example 2

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

  <Canvas Height="200" Width="200" Background="LightBlue">
        <Canvas Canvas.Top="0"   Canvas.Left="0"   Height="100" Width="100" Background="Red"/>
        <Canvas Canvas.Top="50"  Canvas.Left="50"  Height="100" Width="100" Background="Blue"/>
        <Canvas Canvas.Top="100" Canvas.Left="100" Height="100" Width="100" Background="Green"/>
  </Canvas>

</Page>

Example 3

Same as above except that ZIndex is used to determine order of appearance.

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

  <Canvas Height="200" Width="200" Background="LightBlue">
        <Canvas ZIndex="3" Canvas.Top="0"   Canvas.Left="0"   Height="100" Width="100" Background="Red"/>
        <Canvas ZIndex="2" Canvas.Top="50"  Canvas.Left="50"  Height="100" Width="100" Background="Blue"/>
        <Canvas ZIndex="1" Canvas.Top="100" Canvas.Left="100" Height="100" Width="100" Background="Green"/>
  </Canvas>

</Page>

Example 4

Canvas allows elements to be drawn outside the canvas bounds:

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Canvas Height="200" Width="200" Background="LightBlue">
        <Canvas Canvas.Top="-20" Canvas.Left="-20" Height="100" Width="100" Background="Red"/>
        <Canvas Canvas.Top="50"  Canvas.Left="50"  Height="100" Width="100"  Background="Blue"/>
        <Canvas Canvas.Top="100" Canvas.Left="100" Height="100" Width="100"  Background="Green"/>
  </Canvas>
</Page>

Example 5

ClipToBounds can be used to clip Canvas children that are drawn outside the Canvas borders:

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Canvas Height="200" Width="200" Background="LightBlue" ClipToBounds="true">
        <Canvas Canvas.Top="-20" Canvas.Left="-20" Height="100" Width="100" Background="Red"/>
        <Canvas Canvas.Top="50"  Canvas.Left="50"  Height="100" Width="100" Background="Blue"/>
        <Canvas Canvas.Top="100" Canvas.Left="100" Height="100" Width="100" Background="Green"/>
  </Canvas>
</Page>

DockPanel

DockPanel defines a panel where you can arrange child elements by docking them along the edges of the DockPanel. Note the following:

Example 1

This example shows the basics of using a DockPanel

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" WindowTitle="DockPanel Sample">
    <DockPanel LastChildFill="True">
 
        <!-- Note how these two elements are docked relative to the top edge of the DockPanel -->
        <Border Height="25" Background="SkyBlue" BorderBrush="Black" BorderThickness="1" DockPanel.Dock="Top">
            <TextBlock Foreground="Black">Dock = "Top"</TextBlock>
        </Border>
        <Border Height="25" Background="SkyBlue" BorderBrush="Black" BorderThickness="1" DockPanel.Dock="Top">
            <TextBlock Foreground="Black">Dock = "Top"</TextBlock>
        </Border>
 
        <!-- This element is docked relative to the bottom edge of the panel -->
       
<Border Height="25" Background="LemonChiffon" BorderBrush="Black" BorderThickness="1" DockPanel.Dock="Bottom">
            <TextBlock Foreground="Black">Dock = "Bottom"</TextBlock>
        </Border>

        <!--  Dock element to the left edge of the DockPanel, taking into account the remaining space taken
        By the top and bottom elements-->

        <Border Width="200" Background="PaleGreen" BorderBrush="Black" BorderThickness="1" DockPanel.Dock="Left">
            <TextBlock Foreground="Black">Dock = "Left"</TextBlock>
        </Border>

        <!-- Last element fills the remaining space -->
       
<Border Background="White" BorderBrush="Black" BorderThickness="1">
            <TextBlock Foreground="Black">This content will "Fill" the remaining space</TextBlock>
        </Border>
    </DockPanel>
</Page>

Example 2

If Height and Width are not specified for DockPanel (see example above), DockPanel sizes to its content and can increase/decrease to accommodate the size of its child elements.

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" WindowTitle="DockPanel Sample">
    <DockPanel Width="300" Height="200" LastChildFill="True">

      Same as  above ...

    </DockPanel>
</Page>

Grid 

A Grid is a Panel-derived class that is used to position elements within rows and columns. In other words, it combines the functionality of absolute positioning and tabular data control. Note the following:

 

 StackPanel

 

 VirtualizingStackPanel

 

 WrapPanel

  

Other Controls 

Border

Example 1

Border draws a border around an element. Border can have only one child. To display multiple child elements, place a Panel element within the parent Border. Place child elements on the Panel. An important layout property often used with Border is Padding. Padding gets or sets a thickness value that describes the amount of space between a Border and its child elements:

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
    <Border Background="LightBlue"
        BorderBrush="Black"
        BorderThickness="2"
        CornerRadius="45"
        Padding="30">

        <Border Background="LightGreen"
                BorderBrush="Blue"
                BorderThickness="2"
                CornerRadius="45"
                Padding="25">
        </Border>
</Border>
</Page>

   

The following shows the results of reducing Padding for the parent Border (LightBlue) to 10:

   

 Example 2

 A canvas (see Canvas) is nested inside a Border element to give the Canvas a border. Note how elements on the Canvas are placed using Canvas attached properties:

<Page  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
    <Border BorderThickness="2"
            BorderBrush="Black"
            HorizontalAlignment="Center"
            VerticalAlignment="Center"
            Width="200"
            Height="100">
            <Canvas Background="LightBlue">
                <TextBlock Canvas.Top="10" Canvas.Left="20">Hello World!</TextBlock>
                <Button Canvas.Top="30" Canvas.Left="20" Content="Click Me" Width="100" />#
            </Canvas>
    </Border>
</Page>

Button

 Example 1

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
 
    <StackPanel>
        <Button Name="btn1" Width="100" Height="30" Margin="5,5,5,5">
            <Image Source="H:\My Documents\My Shapes\_private\folder.ico"></Image>
        </Button>
 
        <Button Name="btn2" Width="100" Height="30" Margin="5,5,5,5" BorderBrush="Black">
            Browse...
        </Button>
    </StackPanel>
</Page>