WPF Layout: Panels

Proper layout and positioning are vital parts of interactive, high-performance and user-friendly Windows applications. In a series of articles, the layout process in WPF is explained. The series starts with an understanding of the WPF layout process. The next part of this series will cover the basics of layout and positioning such as size, margin, padding and alignment of elements. Later in this series, I will cover various panels and related parent controls available in WPF.

Table of Contents

Introduction

In the previous article, WPF Layout: Dealing with Percentage Size in WPF, I discussed how to deal with the size ration based on the screen size. Now, I switch my focus to panels. This article focuses on the basic understanding of various panels in WPF.

Panels

All Panel controls are defined in the System.Windows.Controls namespace that resides in presentationframework.dll assembly. Besides the root Window, a Panel is the base control that works as a parent control for other child controls. If you create a WPF application using Visual Studio 2008, the default code for the Window looks as in Listing 15, where you can see a Grid panel is the default parent control for child controls that may be placed on a Window.

Listing 1

Listing 1 generates Figure 1.



                                Figure 1

WPF comes with the following five built-in panels:

  • Canvas
  • DockPanel
  • Grid
  • StackPanel
  • WrapPanel

The purpose and use of these panels is different. Each panel has a different way to position and reposition child controls placed within that panel. The following articles in this series will summarise these panels and their usages.

Similar to any other WPF control, a Panel control may be represented in two ways. First, at design-time using XAML elements and attributes, and second, at run-time, using a WPF class and its properties.

The code snippet in Listing 2 creates a Grid panel at design-time using XAML.

  1. <Window x:Class="CanvasPanelSample.Window1"  
  2.    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.    Title="Window1" Height="300" Width="300"  
  5.    Name="RootWindow">  
  6.    <Grid Name="GridPanel" Background="Blue"  
  7.       Width="250" Height="200"  
  8.       VerticalAlignment="Top"  
  9.       HorizontalAlignment="Left"  
  10.       FlowDirection="LeftToRight"  
  11.    />  
  12. </Window>  

Listing 2

Listing 2 generates Figure 2.



                                 Figure 2

The code snippet in Listing 3 creates exactly the same Grid panel shown in Figure 2 at run-time using WPF classes.

  1. private void CreateDynamicPanel()  
  2. {  
  3.   
  4.    // Create a Grid Panel control  
  5.    Grid gridPanel = new Grid();  
  6.   
  7.    // Set Grid Panel properties  
  8.    gridPanel.Background = new SolidColorBrush(Colors.Blue);  
  9.    gridPanel.Width = 250;  
  10.    gridPanel.Height = 200;  
  11.    gridPanel.HorizontalAlignment = HorizontalAlignment.Left;  
  12.    gridPanel.VerticalAlignment = VerticalAlignment.Top;  
  13.    gridPanel.FlowDirection = FlowDirection.LeftToRight;  
  14.   
  15.    // Set Grid Panel as content of the Window  
  16.    RootWindow.Content = gridPanel;  
  17.   
  18. }  

Listing 3

All Panel controls are derived from the Panel class. Let's have a look at the Panel class properties and methods.

Panel Class Properties

The Background property represents the background color of a panel using a Brush object.

The Children property represents all child controls of a Panel.

The HasLogicalOrientation property represents whether a Panel arranges its descendants in a single dimension.

The InternalChildren property represents all child controls of a Panel, including items added directly in the code and also items that are generated by data binding.

The IsItemHost property represents if this Panel is a container for UI items that are generated by an ItemsControl. An ItemsControl is a control that can be used to present a collection of items.

The LogicalChildrent property returns an enumerator that can iterate the logical child elements of this panel.

The LogicalOrientation property represents if a panel only supports layout in a single dimension.

The VisualChildrentCount property represents the number of child Visual objects in this Panel.

Summary

In this article, I discussed basics of the panels in WPF. In the next article of this series, I will focus on the Canvas panel in details.