Learn About A Wrap Panel In WPF

Introduction

 
Wouldn't it be great to have a panel who arranges its child elements by itself? Well, WPF has you covered. In WPF, we can use WrapPanel: The definition is in the name itself, It wraps the contents until there is no empty place left.
 
The WrapPanel arranges its child controls next to one other, either horizontally or vertically, unlike a stack panel by default WrapPanel's orientation is horizontal. While wrapping these child controls, one need to have fix height or width for every child control

If the orientation of WrapPanel is Horizontal, then child controls will share the same height as the highest control. If the orientation of WrapPanel is Vertical, then child controls will share the same width as the widest control

Let's see the first example with default orientation, horizontal

We are going to have 3 labels & 3 TextBoxes & 1 Register button on our UI. 
  1. <Window x:Class="A.MainWindow"    
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    
  3.         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"    
  4.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    
  5.         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"    
  6.         mc:Ignorable="d"    
  7.         Title="MainWindow" Height="220" Width="350"   >    
  8.     <Window.Resources>    
  9.         <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>    
  10.     </Window.Resources>    
  11.     <WrapPanel x:Name="GridOuter"    
  12.                 Grid.IsSharedSizeScope="True">    
  13.             
  14.         <Grid x:Name="GridInnerUserName">    
  15.             <Grid.ColumnDefinitions>    
  16.                 <ColumnDefinition SharedSizeGroup="FirstColumn"/>    
  17.                 <ColumnDefinition SharedSizeGroup="SecondColumn"/>    
  18.             </Grid.ColumnDefinitions>    
  19.             <Label x:Name="LabelUserName"        
  20.                Content="User Name:"     
  21.                Margin="0 10 0 0"/>    
  22.             <TextBox x:Name="TextBoxUserName"      
  23.                  Text="{Binding UserName}"    
  24.                  Height="20"        
  25.                  Width="150"     
  26.                  Grid.Column="1"/>    
  27.         </Grid>    
  28.     
  29.         <Grid x:Name="GridInnerPassword">    
  30.             <Grid.ColumnDefinitions>    
  31.                 <ColumnDefinition SharedSizeGroup="FirstColumn"/>    
  32.                 <ColumnDefinition SharedSizeGroup="SecondColumn"/>    
  33.             </Grid.ColumnDefinitions>    
  34.             <Label x:Name="LabelPassword"         
  35.                Content="Password:" />    
  36.             <PasswordBox  x:Name="TextBoxPassword"        
  37.                  Height="20"        
  38.                  Width="150"    
  39.                  Grid.Column="1"/>    
  40.         </Grid>    
  41.     
  42.         <Grid x:Name="GridInnerEmail">    
  43.             <Grid.ColumnDefinitions>    
  44.                 <ColumnDefinition SharedSizeGroup="FirstColumn"/>    
  45.                 <ColumnDefinition SharedSizeGroup="SecondColumn"/>    
  46.             </Grid.ColumnDefinitions>    
  47.             <Label x:Name="LabelEmailId"         
  48.                Content="Email:" />    
  49.             <TextBox x:Name="TextBoxEmail"        
  50.                  Height="20"        
  51.                  Width="150"     
  52.                  Grid.Column="1"/>    
  53.         </Grid>    
  54.             
  55.         <Button x:Name="ButtonLogin"        
  56.                 Height="20"        
  57.                 Width="100"        
  58.                 Content="Register"        
  59.                 HorizontalAlignment="Center"        
  60.                 Margin="20 10 0 0"      
  61.                 Command="{Binding RegisterButtonClicked}"    
  62.                 />    
  63.     </WrapPanel>    
  64. </Window>    
Run this project, and you'll be able to do this:
 
Wrap Panel In WPF
 
Now if we need to see its horizontal wrapping behaviour, for that we have to change the window size to Height="80" Width="1200" and wrap panel's orientation to vertical Orientation="Vertical" 
  1. <Window x:Class="A.MainWindow"      
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"      
  3.         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"      
  4.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"      
  5.         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"      
  6.         mc:Ignorable="d"      
  7.         Title="MainWindow" Height="80" Width="1200">      
  8.     <Window.Resources>      
  9.         <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>      
  10.     </Window.Resources>      
  11.     <WrapPanel x:Name="GridOuter"     
  12.         Orientation = "Vertical"     
  13.                 Grid.IsSharedSizeScope="True">     
Alright, once you are done with making these changes, run the project & see the alternate magic:
 
Wrap Panel In WPF
 

Conclusion


In this article, we learned how controls can dynamically be arranged.

In layman terms, items are horizontally arranged when the window is stretch horizontally, or items are vertically arranged when the window is stretch vertically.
WPF has rich controls to support rich & dynamic UI.

Thank you for visiting this article. I sure hope you have gained some insight into Wrap panel's behavior.

You can connect with me @