Class CardGrid
A responsive gallery that flows Card tiles into as many columns as the available width allows.
Implements
Inherited Members
Namespace: Unity.AppUI.UI
Assembly: Unity.AppUI.dll
Syntax
[UxmlElement]
public class CardGrid : BaseVisualElement, IEventHandler, IResolvedStyle, ITransform, ITransitionAnimations, IExperimentalFeatures, IVisualElementScheduler, IContextOverrideElement, IAdditionalDataHolder
Remarks
CardGrid is the UITK counterpart of a CSS repeat(auto-fill, ...) grid. UI Toolkit has no CSS
grid and no gap, so the layout is computed instead: the grid is a wrapping flex row, and on
every resize it works out how many columns of at least minItemWidth fit, then
writes a width and the inter-tile margins onto each child. The result matches the web
component's behaviour, including the ragged trailing row.
fill picks between the two column strategies:
with true (the default) the columns stretch to share the leftover width, so tiles only change
size when the column count changes; with false every column is exactly
minItemWidth wide and the row is left-packed, so tile size tracks
minItemWidth continuously.
The gap stays a token rather than a C# constant. CardGrid.uss sizes a hidden, absolutely
positioned probe element from a density-aware --appui-sizing-* step, and the grid reads the
gap back off that probe's resolved width. It therefore rescales with Panel.density on its own
and never appears as a literal in code. (UITK cannot read a px length back through a float
custom property, which is why the probe exists.)
The grid is layout-direction aware. Under .appui--rtl the row itself flips to
row-reverse in USS, so tile 0 sits against the right edge, and Reflow() writes the
inter-tile gap onto margin-left instead of margin-right so it stays on the trailing edge of
each tile. The column arithmetic is direction-agnostic, so column count and tile widths are
identical in both directions.
Note: because CardGrid writes width, margin-left, margin-right and margin-bottom
onto its children, do not set those four inline on a tile — they are overwritten on every
re-flow. Everything else, including the tile height, is yours.
Examples
A template gallery in UXML.
<appui:CardGrid min-item-width="240">
<appui:Card interactive="true">
<appui:CardHeader><appui:CardTitle text="2D Platformer" /></appui:CardHeader>
</appui:Card>
<appui:Card interactive="true">
<appui:CardHeader><appui:CardTitle text="First Person" /></appui:CardHeader>
</appui:Card>
</appui:CardGrid>
Fixed-width columns, built in C#.
var grid = new CardGrid { minItemWidth = 160, fill = false };
foreach (var template in templates)
{
var card = new Card { interactive = true };
var header = new CardHeader();
header.Add(new CardTitle { text = template.name });
card.Add(header);
grid.Add(card);
}
Constructors
CardGrid()
Default constructor.
Declaration
public CardGrid()
Fields
gapProbeUssClassName
The styling class of the hidden element the grid measures its inter-tile gap from.
Declaration
public const string gapProbeUssClassName = "appui-card-grid__gap-probe"
Field Value
| Type | Description |
|---|---|
| string |
ussClassName
The CardGrid main styling class.
Declaration
public const string ussClassName = "appui-card-grid"
Field Value
| Type | Description |
|---|---|
| string |
Properties
fill
Whether the columns stretch to fill the row.
Declaration
[Tooltip("Whether the columns stretch to share the leftover width.")]
[CreateProperty]
[UxmlAttribute]
public bool fill { get; set; }
Property Value
| Type | Description |
|---|---|
| bool |
Remarks
true (the default) shares the leftover width between the columns, so the trailing edge is
flush. false pins every column to minItemWidth and leaves the leftover width
unused at the end of the row.
minItemWidth
The per-item width, in pixels.
Declaration
[Tooltip("The minimum width of a tile before the grid drops a column.")]
[CreateProperty]
[UxmlAttribute]
public float minItemWidth { get; set; }
Property Value
| Type | Description |
|---|---|
| float |
Remarks
With fill this is the minimum width a tile may have before the grid drops a column; without it, it is the exact column width.
resolvedColumnCount
The number of columns the grid last flowed into. 1 until the grid has been laid out.
Declaration
public int resolvedColumnCount { get; }
Property Value
| Type | Description |
|---|---|
| int |
resolvedGap
The gap currently applied between tiles, in resolved pixels.
Declaration
public float resolvedGap { get; }
Property Value
| Type | Description |
|---|---|
| float |
Remarks
Read-only, and driven by the --appui-card-grid-gap custom property, so it changes with the
panel density rather than with any C# setting.
resolvedLayoutDirection
The layout direction the grid last flowed in.
Declaration
public Dir resolvedLayoutDirection { get; }
Property Value
| Type | Description |
|---|---|
| Dir |
Remarks
Read-only, and driven by the nearest DirContext provider, usually the Panel. In Rtl the tiles flow from the right edge and the gap moves to each tile's left side.
Methods
Reflow()
Recomputes the column count and re-applies the tile widths and margins.
Declaration
public void Reflow()
Remarks
This runs automatically whenever the grid resizes, its density changes or one of its properties is set. Call it directly only after mutating the children outside a layout pass.