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 example. You can specify a margin on the Grid. By default, the Grid has no margin and it expands to fill its container area.
Info You don't need to set the Grid's width or height to fill the container. A margin can make the grid smaller.
Detail The syntax for the margin attribute is important. We specify the left, top, right and bottom margins in that order.
Tip We can also omit some margin numbers. We can use 1, 2, or 4 numbers. It starts from the left, and mirror on the opposite sides.
Background. 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.
Info In the 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.
Note With a GridSplitter, we create adjustable user interfaces. These interfaces are more versatile than ones that cannot resize.
Info In Windows Forms, we have the TableLayoutPanel to organize controls in this way. The Grid serves a similar purpose.
Summary. 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.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Apr 15, 2025 (edit).