VariableSizedWrapGrid in Windows Store Apps

The VariableSizedWrapGrid is a panel that can create a typical title-based layout as was introduced with Windows 8. The VariableSizedWrapGrid is only available in WinRT. The VariableSizedWrapGrid arranges its children in row and columns.

For example:

  1. <Page  
  2.     x:Class="App1.MainPage"  
  3.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  4.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  5.     xmlns:local="using:App1"  
  6.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  7.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  8.     mc:Ignorable="d">  
  9.   
  10.     <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">  
  11.          
  12.         
  13.         <VariableSizedWrapGrid   
  14.                                   
  15.                                 >  
  16.             <Button Content="A"></Button>  
  17.             <Button Content="B" Background="Red"></Button>  
  18.             <Button Content="C"></Button>  
  19.             <Button Content="D"></Button>  
  20.             <Button Content="E"></Button>  
  21.             <Button Content="F"></Button>  
  22.         </VariableSizedWrapGrid>  
  23.     </Grid>  
  24. </Page  


It has an Orientation-Property that is by default vertical. That means it stacks children in columns. You can set it to Horizontal to stack the children in rows.
  1. <VariableSizedWrapGrid Orientation="Horizontal" >  
  2.             <Button Content="A"></Button>  
  3.             <Button Content="B" Background="Red"></Button>  
  4.             <Button Content="C"></Button>  
  5.             <Button Content="D"></Button>  
  6.             <Button Content="E"></Button>  
  7.             <Button Content="F"></Button>  
  8. </VariableSizedWrapGrid>  


Besides the Orientation-Property, It has the Properties ItemWidth and ItemHeight to define an equal size for all its children.

For example:
  1. <VariableSizedWrapGrid ItemWidth="200" ItemHeight="100"Orientation="Horizontal"  >  
  2.         <Rectangle Fill="LightGray" />  
  3.         <Rectangle Fill="LightBlue" />  
  4.         <Rectangle Fill="Tomato"  />  
  5.         <Rectangle Fill="Purple" />  
  6.         <Rectangle Fill="Yellow" />  
  7.         <Rectangle Fill="Red"/>  
  8.         <Rectangle Fill="Green"  />  
  9.         <Rectangle Fill="Blue" />  
  10.         <Rectangle Fill="Orange" />  
  11.  </VariableSizedWrapGrid>   


It has another important property, the MaximumRowsOrColumns property. With that property, you define when a break occurs. A vertical VeriableSizedWrapGrid will break to a new column, either when the number of rows is reached that you have defined in the MaximumRowsOrColumns property or when there is no more vertical space left in that column. In a horizontal VariableSizedWrapGrid, the children are stacked in a row. It will break to a new row when the number of columns has reached the number you have defined in the MaximumRowsOrColumns property or when where is no more horizontal space left.

Vertical
  1. <VariableSizedWrapGrid   
  2.                      ItemWidth="200"  
  3.                     ItemHeight="100"  
  4.                                      MaximumRowsOrColumns="2"  
  5.                      Orientation="Vertical"  >  
  6.       <Rectangle Fill="LightGray" />  
  7.       <Rectangle Fill="LightBlue" />  
  8.       <Rectangle Fill="Tomato"  />  
  9.       <Rectangle Fill="Purple" />  
  10.   
  11.       <Rectangle Fill="Yellow" />  
  12.       <Rectangle Fill="Red"  
  13.              
  14.              />  
  15.       <Rectangle Fill="Green"  />  
  16.       <Rectangle Fill="Blue" />  
  17.       <Rectangle Fill="Orange" />  
  18.   </VariableSizedWrapGrid>  


Let's set the property to the value 2. Now you can see in the Designer the VariableSizedWrapGrid has now only two rows. After the two rows are reached, it breaks to a new column.

Horizontal
  1. <VariableSizedWrapGrid   
  2.                        ItemWidth="200"  
  3.                       ItemHeight="100"  
  4.                                        MaximumRowsOrColumns="2"  
  5.                        Orientation="Horizontal"  >  
  6.         <Rectangle Fill="LightGray" />  
  7.         <Rectangle Fill="LightBlue" />  
  8.         <Rectangle Fill="Tomato"  />  
  9.         <Rectangle Fill="Purple" />  
  10.   
  11.         <Rectangle Fill="Yellow" />  
  12.         <Rectangle Fill="Red"  
  13.                
  14.                />  
  15.         <Rectangle Fill="Green"  />  
  16.         <Rectangle Fill="Blue" />  
  17.         <Rectangle Fill="Orange" />  
  18.     </VariableSizedWrapGrid>  


Now you can see in the Designer the VariableSizedWrapGrid has now only two columns. After the two columns are reached, it breaks to a new row.
All right, now let's look at the attached properties RowSpan and ColumnSpan.

For example:
  1. <VariableSizedWrapGrid   
  2.                        ItemWidth="100"  
  3.                       ItemHeight="100"  
  4.                        Orientation="Vertical" >  
  5.         <Rectangle Fill="Khaki" />  
  6.         <Rectangle Fill="LightBlue" />  
  7.         <Rectangle Fill="Tomato" VariableSizedWrapGrid.ColumnSpan="2" />  
  8.         <Rectangle Fill="Purple" />  
  9.   
  10.         <Rectangle Fill="Yellow" VariableSizedWrapGrid.ColumnSpan="2" />  
  11.         <Rectangle Fill="Red"  
  12.                
  13.                VariableSizedWrapGrid.RowSpan="2" />  
  14.         <Rectangle Fill="Green" VariableSizedWrapGrid.RowSpan="2" />  
  15.         <Rectangle Fill="Blue" />  
  16.         <Rectangle Fill="Orange" />  
  17.     </VariableSizedWrapGrid>  


Summary

This article explained the VariableSizedWrapGrid with the Orientation, ItemWidth, ItemHeight MaximumRowsOrColumns, RowSpan and ColumnSpan properties. Thanks for reading.


Similar Articles