XAML WrapPanel

XAML WrapPanel Tutorial
 
A parent control with multiple child elements may require wrapping functionality to place child elements in an order. The WrapPanel element in XAML is used to provide vertical or horizontal wrapping of child elements of the panel.
 
The XAML WrapPanel element a wrap panel. The Orientation property is used to provide wrapping functionality of child elements. It can either be horizontally or vertically. 
  1. <WrapPanel Width="300" Height="200"  
  2.            Background="LightBlue" />  
The following code snippet sets the Orientation of a WrapPanel to Horizontal.
  1. <WrapPanel Orientation=”Horizontal”>    
  2.    Children    
  3. </WrapPanel>   
Listing 1 is the complete code that defines a WrapPanel and sets the Orientation, ItemHeight, and ItemWidth properties of a WrapPanel in XAML. This below code will have every item of a WrapPanel have height and width 100 respectively. 
  1. <Window x:Class="WrapPanelSample.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="400" Width="539.426" Name="RootWindow">  
  5.   
  6.   <WrapPanel Width="300" Height="200"  
  7.            Background="LightBlue" Orientation="Vertical"   
  8.            ItemHeight="100" ItemWidth="100" />  
  9. </Window>  
Listing 1.
 
Note: If you do not specify ItemHeight and ItemWidth properties of a WrapPanel, the default height and width of child items within a WrapPanel will be child elements height and width.
 
The code example in Listing 2 adds 6 child elements to a Wrap Panel. 
  1. <WrapPanel>  
  2.     <Ellipse Width="100" Height="100" Fill="Red" />  
  3.     <Separator Width="10" />  
  4.     <Button Background="Orange" Width="180" Height="50"  
  5.             FontFamily="Georgia" FontSize="18"   
  6.             FontWeight="Bold" Name="ClickMeButton" >Click Me button</Button>  
  7.     <Separator Width="10" />  
  8.     <TextBox Height="50" Width="200" Name="Button1" />  
  9.     <Rectangle Width="100" Height="100" Fill="Green" />  
  10. </WrapPanel>  
Listing 2.
 
The output of Listing 2 looks the following Figure.
 
XAML Wrap Panel 
 
We can also use nested Wrap panels. The code snippet in Listing 3 adds a child WrapPanel to a parent WrapPanel. The child WrapPanel has 5 Button elements stacked vertically. 
  1. <Window x:Class="WrapPanelSample.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="400" Width="700" Name="RootWindow">  
  5.   
  6. <WrapPanel>  
  7.     <Ellipse Width="100" Height="100" Fill="Red" />  
  8.     <Separator Width="10" />  
  9.     <Button Background="Orange" Width="180" Height="50"  
  10.             FontFamily="Georgia" FontSize="18"   
  11.             FontWeight="Bold" Name="ClickMeButton" >Click Me button</Button>  
  12.     <Separator Width="10" />  
  13.     <TextBox Height="50" Width="200" Name="Button1" />  
  14.     <Separator Width="10" />  
  15.     <Rectangle Width="100" Height="100" Fill="Green" />  
  16.         <Separator Width="10" />  
  17.   
  18.         <!-- Child WrapPanel-->  
  19.         <WrapPanel Orientation="Vertical" >  
  20.             <Button Height="50" Width="200" >Button1</Button>  
  21.             <Button Height="50" Width="200" >Button1</Button>  
  22.             <Button Height="50" Width="200" >Button1</Button>  
  23.             <Button Height="50" Width="200" >Button1</Button>  
  24.             <Button Height="50" Width="200" >Button1</Button>  
  25.         </WrapPanel>  
  26.     </WrapPanel>  
  27.   
  28. </Window>  
Listing 3.
 
The output of Listing 3 looks like the following.
 
Nested XAML Wrap Panel 
 
Learn more about how to use a WrapPanel in a WPF app.
 
 


Recommended Free Ebook
Similar Articles
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.