Introduction
In this article we are talking about Layouts in Windows Store Apps. We develop either for Windows Forms or Windows Store Apps. In both of the cases, developers rely on a container (and, you say layouts). Since it doesn't affect much of the design part, but it helps developers to do their design.
Talking about Silverlight, we have many Layout controls to use but in Windows Store we generally use Grid and Stack Panel.
So, our Agenda will be:
- Prime knowledge about Layouts
- Implement Grid on blank XAML page
- Then, use Stack Panel inside grid
Background
Before starting your App development, you should keep the basic layout in your mind. You can design your layout by a Grid that is similar to a tabular view. So, your first step will be the Grid's definition.
Whereas a Stack Panel is something like a stack of items and it can be horizontally aligned or vertically aligned (in other words one after another).
Procedure
So, let's do a Grid demo.
- <Grid>
- <Grid.RowDefinitions>
- <RowDefinition/>
- <RowDefinition/>
- </Grid.RowDefinitions>
- <Grid.ColumnDefinitions>
- <ColumnDefinition/>
- <ColumnDefinition/>
- <ColumnDefinition/>
- </Grid.ColumnDefinitions>
-
- <TextBlock Grid.Row="0" Text="Layout Demo" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="80" Foreground="White"/>
- <Rectangle Grid.Column="0" Grid.Row="1" Fill="Magenta"/>
- <Rectangle Grid.Column="1" Grid.Row="1" Fill="Cyan"/>
- <Rectangle Grid.Column="2" Grid.Row="1" Fill="Lime"/>
-
- </Grid>
In this Grid definition, we have 2x3 grids (in other words 2 Rows and 3 Columns).
Further, we add three colored rectangles to all three columns. And it looks like:
Now, add some Stack Panels in each of the columns.
- <!-- FIRST COLLUMN -->
- <StackPanel Orientation="Vertical" Grid.Row="1" Grid.Column="0" Margin="20">
- <Button Content="One" HorizontalAlignment="Center" FontSize="30" Width="400" Height="100"/>
- <Button Content="Two" HorizontalAlignment="Center" FontSize="30" Width="400" Height="100"/>
- <Button Content="Three" HorizontalAlignment="Center" FontSize="30" Width="400" Height="100"/>
- </StackPanel>
-
- <!-- SECOND COLLUMN-->
- <StackPanel Orientation="Horizontal" Grid.Row="1" Grid.Column="1" Margin="20">
- <Button Content="One" HorizontalAlignment="Center" FontSize="30" Width="200" Height="200"/>
- <Button Content="Two" HorizontalAlignment="Center" Width="200" FontSize="30" Height="200"/>
- </StackPanel>
-
- <!-- THIRD COLLUMN -->
- <StackPanel Orientation="Vertical" Grid.Row="1" Grid.Column="2" Margin="20">
- <Button Content="One" FontSize="30" HorizontalAlignment="Center" Width="400" Height="100"/>
- <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
- <Button Content="Two" HorizontalAlignment="Center" Width="100" Height="100"/>
- <Button Content="Two" HorizontalAlignment="Center" Width="100" Height="100"/>
- <Button Content="Two" HorizontalAlignment="Center" Width="100" Height="100"/>
- <Button Content="Two" HorizontalAlignment="Center" Width="100" Height="100"/>
- </StackPanel>
- <Button Content="One" FontSize="30" HorizontalAlignment="Center" Width="400" Height="100"/>
- </StackPanel>
When you use a Stack Panel you must take care of Orientation and it Horizontal alignment/ Vertical Alignment. The outcome will be:
Conclusion
Maintaining a good layout is as important as coding. From a designer's point of view, it takes your app from rags to riches.