dotnet / dotnet/maui

Simplify Grid definitions, with VerticalGrid and HorizontalGrid

Open
#6,726 9 comments 1 reaction 0 assignees View on GitHub
area-layout proposal/open
Dominant language
C#
Stars
23.3k
Forks
2k
Avg merge
1d 15h
Merged PRs (30d)
290

Description

## Description

MAUI simplifies stack layouts with `VerticalStackLayout` and `HorizontalStackLayout`, allowing for more concise, readable XAML.
The proposal here is to do something similar for the other common layout type: `Grid`, introducing `VerticalGrid` and `HorizontalGrid`.

A `VerticalGrid` is a list of rows, while a `HorizontalGrid` is a list of columns. They have these advantages over classic `Grid`:

- Allows grid definitions without a need to keep track of row/column indices - the numbering is implicit. Rows/columns can be most easily added/removed.
- Produces XAML markup that more closely matches the actual grid structure - each row/column is a separate indented group, making the grid easier to read and understand.

## Public API Changes

### Current Syntax
Here's a typical grid today:

```




```

### Proposed Syntax
The same grid as above with the proposed syntax is below:

```








```

Note that there's no longer a need to specify Grid.Row / Grid.Column, as the row/column index are computed automatically.

Occasionally, a grid is better conceptualized as a list of columns. In that case, it can be expressed with `HorizontalGrid`:

```








```

### Handling Row/Column spans

If a Row element has a child cell that spans adjacent columns, that's expressed like with a traditional `Grid`:
```









```

If a Row element has a child cell that spans rows below it, it's expressed as:
```










```

Using the `SpannedCell` dummy element helps makes the grid structure explicit - a cell would have been here, but instead its contents are provided by a row above. In both cases you look at a single Row and see how the children map to columns.

Similarly, an `EmptyCell` can be used for cases a grid cell is intentionally blank, without spanning:
```










```

The rules above mean that tooling can (ideally) enforce that every row/column has the expected number of children, helping minimize mistakes. Row/column indices aren't used with `VerticalGrid`/`HorizontalGrid`.

## Prior Art

* The Flutter Table widget is similar to `VerticalGrid`. It contains a list of `TableRow` elements. Flutter doesn't have an equivalent to `HorizontalGrid`.
* HTML tables are a list of `tr` (table row) elements. HTML developers should find this new Grid syntax intuitive, as it's the same model.
* Comet has [VGrid and HGrid](https://twitter.com/jtclancey/status/1478452660046995457), which is where I got the idea originally. Comet's syntax is more concise, as it omits the Row and Column elements, making them implicit based on item index. But I prefer the explicit Row/Column elements, especially for XAML, as it makes the grid structure more obvious and editing less error prone.

## Intended Use-Case

Just as `VerticalStackLayout` / `HorizontalStackLayout` will likely replace most uses of `StackLayout`, it's expected that most uses of `Grid` will move to using `VerticalGrid` / `HorizontalGrid` instead, given its advantages.

Contributor guide

Open the contributing guide

Research direction

The issue names no files, tests, or implementation entry points. Start by locating the existing Grid, VerticalStackLayout, and HorizontalStackLayout APIs and their XAML behavior; done would require implementing and validating the proposed VerticalGrid and HorizontalGrid model, including spans, empty cells, and implicit indices.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
frontend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.