Difference between Grid and StackPanel
Grid VS Stack Panel
Are you new to WPF and confused about using grid and stack panel? If you answered yes then you are at the right place.
When I started WPF development, I was new to XAML and the first confusion in my mind was about the usage of Grid and Stack panel. What’s the difference and which one to use?
Let me guide you about this. At past layouts used to be simple but now we want them adaptive to make them able to run on multiple devices. First of all, keep in mind that both are containers.
Containers are used for positioning in windows platform foundation controls. They have children property so they can hold multiple elements. By embedding any control inside of a layout control, you are implicitly calling the Add method of the Children collection property.
Grid:
When you create a new project, grid is there for you by default. This grid has one row and one column and make it a one large cell covering the whole space. Grid is like a matrix and you can place your element by telling it the row and column.
In this example I will make a grid of 3x3 means 9 cells and then we can place our elements in any particular cell.
Make three rows and columns by writing the following code:
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
Height and width is used to define the size of a particular row and column and any cell can have any size.
Size can be expressed in following three ways:
1) Explicit pixels but not preferred – 100
2) Auto - use the largest value of elements it contains to define the width / height
3) * (Star Sizing) - Utilize all the available space
- 1* - Of all available space, will be given 1 "share"
- 2* - Of all available space, will be given 2 "shares"
- 3* - Of all available space, will be given 3 "shares"
- * - of all available space, will be given whole share
This following code will make the matrix having numbers in them and you can access them by using Grid.Row="" & Grid.Column="" properties and can have desired elements in it
<Page.Resources>
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="42" />
<Setter Property="Foreground" Value="Black" />
</Style>
</Page.Resources>
<Grid Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock>1</TextBlock>
<TextBlock Grid.Column="1">2</TextBlock>
<TextBlock Grid.Column="2">3</TextBlock>
<TextBlock Grid.Row="1">4</TextBlock>
<TextBlock Grid.Row="1" Grid.Column="1">5</TextBlock>
<TextBlock Grid.Row="1" Grid.Column="2">6</TextBlock>
<TextBlock Grid.Row="2">7</TextBlock>
<TextBlock Grid.Row="2" Grid.Column="1">8</TextBlock>
<TextBlock Grid.Row="2" Grid.Column="2">9</TextBlock>
</Grid>
This will generate the following output:
https://4.bp.blogspot.com/-RimYGQK1fJA/VnJ5t53NskI/AAAAAAAAAT4/seSSblrTkwc/s640/Stackpanelvsgrid.JPG
Stack Panel:
Stack Panel acts like a stack of things placed one after another. It can be horizontal or vertical. Unlike Grid you cannot access particular place in a stack panel, every next element will be placed after one another in a sequence.
Stack Panel has Vertical Orientation by default and Left to right flow by default if selected horizontal orientation.
The following code will generate a stack panel having three colored rectangles stacked vertically on each other.
<StackPanel Orientation="Vertical">
<Rectangle Height="200" Width="Auto" Fill="Yellow"></Rectangle>
<Rectangle Height="200" Width="Auto" Fill="Green"></Rectangle>
<Rectangle Height="200" Width="Auto" Fill="Blue"></Rectangle>
</StackPanel>
As vertical orientation is by default so there is no need to mention the orientation. This code will generate the following output
https://3.bp.blogspot.com/-34S85VmIozg/VnJ5t9Ll65I/AAAAAAAAAUA/jX2p8hSGGwY/s400/SP.JPG
The following code will generate stack panel in horizontal orientation:
<StackPanel Orientation="Horizontal">
<Rectangle Height="Auto" Width="100" Fill="Yellow"></Rectangle>
<Rectangle Height="Auto" Width="100" Fill="Green"></Rectangle>
<Rectangle Height="Auto" Width="100" Fill="Blue"></Rectangle>
</StackPanel>
This will have the following output:
https://4.bp.blogspot.com/-Y0nvxmczcoM/VnJ5t2FR3bI/AAAAAAAAATw/BjTylmQjJLc/s400/SP_H.JPG
Conclusion:
As you can observe clearly by yourself the difference between these all, now you can decide which one to use when.
Stack panels can have stack panels inside a stack panel which can be combined to make beautiful layouts. Stack panels are prefer when making lists type layout so each element can be stacked one over and other and they can be adjust on multiple screens accordingly.
Grids are used when you want to define particular cell and don’t want your element to be in a sequence.
Grid will overlap controls but StackPanel will stack them. Both have their own usage and you can make choice according to your needs.