WrapPanel in WPF

A WrapPanel is very similar to a StackPanel because a WrapPanel stacks child elements side by side or stacked upon each other. In addition, a WrapPanel wraps the Child elements to the next row or column when there is no available space.
Let's create various layouts using a WrapPanel.
 
The following describes a sample of creating a simple layout using a Wrapanel. The default Orientation of a WrapPanel is Horizontal and child elements are arranged from left to right. The ItemHeight and ItemWidth property of a WrapPanel sets a uniform height and width of child elements. If we reduce the size of the window then child elements are moved to the next line or row.
  1. <Window x:Class="WrapPanelExample1.MainWindow"  
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.         Title="WrapPanel" Height="350" Width="525">  
  5.     <WrapPanel ItemHeight="60" ItemWidth="100">  
  6.         <Button Content="Button 1" Margin="10"></Button>  
  7.         <Button Content="Button 2" Margin="10"></Button>  
  8.         <Button Content="Button 3" Margin="10"></Button>  
  9.         <Button Content="Button 4" Margin="10"></Button>  
  10.         <Button Content="Button 5" Margin="10"></Button>  
  11.         <Button Content="Button 6" Margin="10"></Button>  
  12.         <Button Content="Button 7" Margin="10"></Button>  
  13.         <Button Content="Button 8" Margin="10"></Button>  
  14.     </WrapPanel>  
  15. </Window>  
Preview
 
The following describes a sample of changing the Orientation of a WrapPanel to Vertical. Setting the Orientation to vertical arranges child elements from top to bottom and if neccessary, child elements are move to the next column.
  1. <WrapPanel ItemHeight="60" ItemWidth="100" Orientation="Vertical">  
  2.         <Button Content="Button 1" Margin="10"></Button>  
  3.         <Button Content="Button 2" Margin="10"></Button>  
  4.         <Button Content="Button 3" Margin="10"></Button>  
  5.         <Button Content="Button 4" Margin="10"></Button>  
  6.         <Button Content="Button 5" Margin="10"></Button>  
  7.         <Button Content="Button 6" Margin="10"></Button>  
  8.         <Button Content="Button 7" Margin="10"></Button>  
  9.         <Button Content="Button 8" Margin="10"></Button>  
  10. </WrapPanel>  
Preview 
 
 
 
The following describes a sample of changing the FlowDirection Property in a WrapPanel. When we change the WrapPanel's FlowDirection property from right to left, then stacking of the child elements start from the right and proceeds to the left. The default behaviour is LeftToRight.
  1. <WrapPanel ItemHeight="60" ItemWidth="100" FlowDirection="RightToLeft">  
  2.         <Button Content="Button 1" Margin="10"></Button>  
  3.         <Button Content="Button 2" Margin="10"></Button>  
  4.         <Button Content="Button 3" Margin="10"></Button>  
  5.         <Button Content="Button 4" Margin="10"></Button>  
  6.         <Button Content="Button 5" Margin="10"></Button>  
  7.         <Button Content="Button 6" Margin="10"></Button>  
  8.         <Button Content="Button 7" Margin="10"></Button>  
  9.         <Button Content="Button 8" Margin="10"></Button>  
  10. </WrapPanel>  
Preview 
 
 
The following describes a sample of setting the VerticalAlignment Property of child elements in a WrapPanel with Horizontal Orientation. We can also adjust the VerticalAlignment Property of child elements in a WrapPanel with Horizontal Orientation and vice-versa.
  1. <WrapPanel ItemHeight="60" ItemWidth="100">  
  2.         <Button Content="Button 1" Margin="10" VerticalAlignment="Stretch"></Button>  
  3.         <Button Content="Button 2" Margin="10" VerticalAlignment="Top"></Button>  
  4.         <Button Content="Button 3" Margin="10" VerticalAlignment="Bottom"></Button>  
  5.         <Button Content="Button 4" Margin="10" VerticalAlignment="Center"></Button>  
  6.         <Button Content="Button 5" Margin="10"></Button>  
  7. </WrapPanel>  
Preview
 
I hope you like it. Thanks. 

Up Next
    Ebook Download
    View all
    Learn
    View all