Grid
A WPF Grid
is an organizational control. With it, we arrange controls in clear, professional ways. We avoid small inconsistencies that might make us look incompetent.
Grids are nested in other controls. When dragging controls around on a Grid
, Visual Studio will helpfully render red bars to guide their placement.
Margin
exampleYou can specify a margin on the Grid
. By default, the Grid
has no margin and it expands to fill its container area.
Grid
's width or height to fill the container. A margin can make the grid smaller.<Window x:Class="WpfApplication5.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid Background="AliceBlue" Margin="10 20 30 40"> </Grid> </Window>
If you leave the Grid
transparent, you can change instead the background of the Window underneath it. But you can specify its background with the Background attribute.
Margin
section on this article, I show the AliceBlue
background. I used AliceBlue
because it is shown first in Visual Studio.GridSplitter
A Grid
can be used with rows and columns (with RowDefinitions
and ColumnDefinitions
). And if you use rows and columns, you can use a GridSplitter
to resize them.
GridSplitter
, we create adjustable user interfaces. These interfaces are more versatile than ones that cannot resize.TableLayoutPanel
to organize controls in this way. The Grid
serves a similar purpose.Controls can be hard to arrange. And if you make a mistake, it will make the entire program look bad. With grid, we arrange controls with help from Visual Studio.