XAML - WrapPanel

In WrapPanel, child elements are positioned in a sequential order from left to right or from top to bottom based on the orientation property. The only difference between StackPanel and WrapPanel is that it doesn’t stack all the child elements into a single line, but it wraps the remaining elements to another line if there is no space left.
WrapPanel is mostly used for tabs or menu items. The hierarchical inheritance of WrapPanel class is as follows −
Background
Gets or sets a Brush that fills the panel content area. (Inherited from Panel)
Children
Gets a UIElementCollection of child elements of this Panel. (Inherited from Panel.)
Height
Gets or sets the suggested height of the element. (Inherited from FrameworkElement.)
ItemHeight
Gets or sets a value that specifies the height of all items that are contained within a WrapPanel.
ItemWidth
Gets or sets a value that specifies the width of all items that are contained within a WrapPanel.
LogicalChildren
Gets an enumerator that can iterate the logical child elements of this Panel element. (Inherited from Panel.)
LogicalOrientation
The Orientation of the panel, if the panel supports layout in only a single dimension. (Inherited from Panel.)
Margin
Gets or sets the outer margin of an element. (Inherited from FrameworkElement.)
Name
Gets or sets the identifying name of the element. The name provides a reference so that code-behind, such as event handler code, can refer to a markup element after it is constructed during processing by a XAML processor. (Inherited from FrameworkElement.)
Orientation
Gets or sets a value that specifies the dimension in which child content is arranged.
Parent
Gets the logical parent element of this element. (Inherited from FrameworkElement.)
Resources
Gets or sets the locally-defined resource dictionary. (Inherited from FrameworkElement.)
Style
Gets or sets the style used by this element when it is rendered. (Inherited from FrameworkElement.)
Width
Gets or sets the width of the element. (Inherited from FrameworkElement.)
The following example shows how to add child elements into a WrapPanel. Here is the XAML implementation to create Text Blocks and Text Boxes inside a WrapPanel in horizontal direction.
<Window x:Class = "XAMLWrapPanel.Window1" 
   xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
   xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" 
   Title = "MainWindow" Height = "300" Width = "604"> 
	
   <Grid> 
      <WrapPanel Orientation = "Horizontal"> 
         <TextBlock Text = "Fist Name" Width = "60" Height = "20" Margin = "5" /> 
         <TextBox  Width = "200" Height = "20" Margin = "5" /> 
         <TextBlock Text = "Last Name" Width = "60" Height = "20" Margin = "5" /> 
         <TextBox  Width = "200" Height = "20" Margin = "5" /> 
         <TextBlock Text = "Age" Width = "60" Height = "20" Margin = "5" /> 
         <TextBox  Width = "60" Height = "20" Margin = "5" /> 
         <TextBlock Text = "Title" Width = "60" Height = "20" Margin = "5" /> 
         <TextBox  Width = "200" Height = "20" Margin = "5" /> 
      </WrapPanel> 
   </Grid> 
	
</Window> 
When the above code is compiled and executed, it will produce the following output. You can change the arrangement from top to bottom by changing the orientation property to Vertical.
We recommend you to execute the above example code and experiment with some other properties as well.
 Print
 Add Notes
 Bookmark this page