WPF Layout: WrapPanel

Proper layout and positioning are a vital part of interactive, high-performance and user-friendly Windows applications. This series of articles explains the layout process in WPF. 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: StackPanel, I discussed the StackPanel. This article focuses on the WrapPanel in details.

WrapPanel

WrapPanel is similar to StackPanel but it has an additional feature. If elements that are stacked horizontally or vertically don't fit in the row or column they are in, the remaining elements will wrap around in the same sequence.

The WrapPanel element in XAML represents a WrapPanel. The following code snippet creates a WrapPanel and sets its height, width and background properties at design-time using XAML.

  1. <WrapPanel Width="300" Height="200" Background="LightBlue" />  
The Orientation property is used to wrap child items horizontally or vertically. The ItemHeight and ItemWidth properties of WrapPanel are used to set a fixed uniform height and width of all items that are contained within a WrapPanel. The following code snippet sets Orientation, ItemHeight and ItemWidth properties of a WrapPanel in XAML. The following code will have every item within the WrapPanel with a height and width of 100 each.
  1. <WrapPanel Width="300" Height="200" Background="LightBlue" Orientation="Vertical" ItemHeight="100" ItemWidth="100" />  
Note: If you do not specify the ItemHeight and ItemWidth properties of a WrapPanel, the default height and width of child items within a WrapPanel will be the height and width of the child elements.

The code snippet in Listing 1 creates a WrapPanel control and keeps its orientation to horizontal by default.
  1. <WrapPanel >  
  2.     <Ellipse Width="100" Height="100" Fill="Red" />  
  3.     <Ellipse Width="90" Height="90" Fill="Orange" />  
  4.     <Ellipse Width="80" Height="80" Fill="Yellow" />  
  5.     <Ellipse Width="70" Height="70" Fill="LightGreen" />  
  6.     <Ellipse Width="60" Height="60" Fill="Green" />  
  7.     <Ellipse Width="50" Height="50" Fill="LightBlue" />  
  8.     <Ellipse Width="40" Height="40" Fill="Blue" />  
  9.     <Ellipse Width="30" Height="30" Fill="Black" />  
  10. </WrapPanel>  

Listing 1

The output looks as in Figure 1 where all the child controls are wrapped horizontally.



Figure 1

Now if you change the orientation to vertical as in the following code, the output looks as in Figure 2.

  1. <WrapPanel Orientation="Vertical">  
As you can see in Figure 2, the new controls are aligned vertically.



Figure 2

The WrapPanel class in WPF represents a WrapPanel. The code listed in Listing 2 creates a StackPanel dynamically, sets its properties and adds five ellipses.
  1. private void CreateDynamicWrapPanel()   
  2. {  
  3.     // Create a WrapPanel and set its properties   
  4.     WrapPanel dynamicWrapPanel = new WrapPanel();  
  5.     dynamicWrapPanel.Background = new SolidColorBrush(Colors.LightBlue);  
  6.     dynamicWrapPanel.Orientation = Orientation.Horizontal;  
  7.     // Create Ellipses and add to StackPanel   
  8.     Ellipse redCircle = new Ellipse();  
  9.     redCircle.Width = 100;  
  10.     redCircle.Height = 100;  
  11.     redCircle.Fill = new SolidColorBrush(Colors.Red);  
  12.     dynamicWrapPanel.Children.Add(redCircle);  
  13.     Ellipse orangeCircle = new Ellipse();  
  14.     orangeCircle.Width = 80;  
  15.     orangeCircle.Height = 80;  
  16.     orangeCircle.Fill = new SolidColorBrush(Colors.Orange);  
  17.     dynamicWrapPanel.Children.Add(orangeCircle);  
  18.     Ellipse yellowCircle = new Ellipse();  
  19.     yellowCircle.Width = 60;  
  20.     yellowCircle.Height = 60;  
  21.     yellowCircle.Fill = new SolidColorBrush(Colors.Yellow);  
  22.     dynamicWrapPanel.Children.Add(yellowCircle);  
  23.   
  24.     Ellipse greenCircle = new Ellipse();  
  25.     greenCircle.Width = 40;  
  26.     greenCircle.Height = 40;  
  27.     greenCircle.Fill = new SolidColorBrush(Colors.Green);  
  28.     dynamicWrapPanel.Children.Add(greenCircle);  
  29.   
  30.     Ellipse blueCircle = new Ellipse();  
  31.     blueCircle.Width = 20;  
  32.     blueCircle.Height = 20;  
  33.     blueCircle.Fill = new SolidColorBrush(Colors.Blue);  
  34.     dynamicWrapPanel.Children.Add(blueCircle);  
  35.   
  36.     // Display WrapPanel into a Window   
  37.     RootWindow.Content = dynamicWrapPanel;  
  38. }  

Listing 2

Summary

In this article, I discussed the WrapPanel in WPF. In the next article of this series, I will focus on Borders of WPF elements in details.


Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.