UI Toolbox logoUI Toolbox
Support

GridView

GridView in UIToolbox shows items in cells that wrap to the width: an icon gallery, a card collection, an inventory, a level select or a list of save slots. It is virtual: only the rows in view, and one row above and one below them, have elements. MakeItem makes the element of a cell once, and BindItem fills it again with another item while the grid scrolls, so thousands of items scroll smoothly and without garbage. The number of columns follows the width, the arrow keys move across and down, and paged shows page buttons in place of the scroll bar.

A gallery of ability icons in rows, with one icon selectedA gallery of ability icons in rows, with one icon selected

Video tutorial: Grid view. All videos: Video tutorials.

<tb:GridView
    name="inventory"
    item-width="80"
    item-height="96"
    spacing="6"
    selection-type="Multiple" />
var grid = root.Q<GridView>("inventory");

grid.MakeItem = () =>
{
    var cell = new VisualElement();
    cell.Add(new VisualElement { name = "icon" });
    cell.Add(new Label { name = "count" });
    return cell;
};
// Called again for other items while the grid scrolls: set everything
// that differs between items
grid.BindItem = (cell, i) =>
{
    cell.Q("icon").style.backgroundImage = items[i].Icon;
    cell.Q<Label>("count").text = items[i].CountText;
};
grid.ItemsSource = items;

grid.SelectionChanged += selected => ShowDetails(selected);
grid.ItemActivated += i => Use(items[i]);

With no MakeItem, each cell is a label with the text of its item, so grid.ItemsSource = names; is enough for a quick grid.

Settings

Setting (UXML) Type Default Does
item-width float 96 The smallest width of a cell, in pixels. The columns are as many as fit in the width.
item-height float 96 The height of a cell, in pixels.
spacing float 8 The space between two cells, across and down.
stretch-items bool true The cells grow so that the columns fill the row. False keeps them item-width wide, from the left.
selection-type SelectionType Single None, Single or Multiple. Multiple selects with Ctrl and Shift.
paged bool false Shows one page at a time, as many items as fit in the height, with a Pagination under the grid in place of the scroll bar.

Set these from code:

Property Does
ItemsSource The items, any IList. A new list shows from the start.
MakeItem Makes the element of a cell. The grid calls it only for the cells in view. Setting it makes the cells again.
BindItem Fills the element of a cell with the item at an index.
UnbindItem Clears the element of a cell before it shows another item, for example to cancel a load. Optional.
SelectedIndex The selected item that the keys move from, or -1. Setting it selects only that item.
SelectedIndices The selected indices, in the order of selection.
ColumnCount The columns that fit in the width (read only).
PageSize The items on one page when paged is on (read only).
Pagination, ScrollView The parts of the grid, for their own settings and events.

Methods

Method Does
RefreshItems() Binds the cells in view again, after the items changed in place or their count changed. Selected indices past the end leave the selection.
Rebuild() Makes the elements of the cells again with MakeItem, then binds them.
ScrollToItem(index) Scrolls so that the item shows. With paged on, it goes to the page of the item.
SetSelection(index) Selects only that item. -1 clears the selection.
AddToSelection(index) Adds an item to the selection. With a single selection it selects only that item.
SelectAll(), ClearSelection() Select all the items (with a multiple selection), or none.

Events

Event When
SelectionChanged The selection changes, by the user or by code. It gives the selected indices.
ItemActivated The user chooses an item: a double click, Enter or Space, or the submit button of a gamepad. It gives the index.

Mouse, keyboard and gamepad

Input Does
Click Selects the item and gives the grid the focus. A touch selects on release, so a drag scrolls without selecting.
Ctrl + click Adds the item to the selection, or takes it out (Multiple).
Shift + click Selects the range from the last clicked item (Multiple).
Double click, Enter, Space, gamepad submit Raises ItemActivated.
Arrows, gamepad stick and d-pad Move across a row and up and down a column. At the edge of the grid the focus moves on to the next control.
Shift + arrows Extends the selection (Multiple).
Home, End The first and the last item.
Page Up, Page Down Move by the rows in view, or by a page when paged is on.
Ctrl + A Selects all (Multiple).
Mouse wheel, touch drag Scroll.

The keys work while the grid itself has the focus. A field inside a cell keeps its own keys.

Loading cells

When the data of an item is not there yet, for example icons that download, show a Skeleton in the cell and swap it for the content when the data comes. Each cell holds both, and BindItem shows one of them:

grid.MakeItem = () =>
{
    var cell = new VisualElement();
    cell.Add(new Skeleton { Shape = SkeletonShape.Block });
    cell.Add(MakeContent());
    return cell;
};
grid.BindItem = (cell, i) =>
{
    var ready = items[i].Loaded;
    cell[0].style.display = ready ? DisplayStyle.None
        : DisplayStyle.Flex;
    cell[1].style.display = ready ? DisplayStyle.Flex
        : DisplayStyle.None;
    if (ready) Fill(cell[1], items[i]);
};

// When more data has come, bind the cells in view again
grid.RefreshItems();

Performance

Animated reflow

Set AnimateReflow (animate-reflow) and the cells that change place spring from the old place to the new one, such as when a new width changes the number of columns. A cell that shows another item after a scroll or a page change jumps. It is off by default. The spring has critical damping, so a cell does not overshoot, and each cell has a small seeded difference in stiffness. One loop moves all the cells and stops when every spring is at rest. With animations off for the workspace (reduce motion), the cells snap at once. A list that changes order binds the items again in the same cells, so the cells do not fly; use a GridPanel to animate a reorder.

view.AnimateReflow = true;
view.Reflow.Stiffness = 300;
view.ReflowCompleted += () => Debug.Log("settled");
Member Description
AnimateReflow (animate-reflow) True to spring the cells to their new places. False by default.
Reflow The Reflow helper while AnimateReflow is on, otherwise null.
ReflowCompleted After every spring came to rest.

Animated reflow

Set AnimateReflow (animate-reflow) and the cells that change place spring from the old place to the new one, such as when a new width changes the number of columns. A cell that shows another item after a scroll or a page change jumps. It is off by default. The spring has critical damping, so a cell does not overshoot, and each cell has a small seeded difference in stiffness. One loop moves all the cells and stops when every spring is at rest. With animations off for the workspace (reduce motion), the cells snap at once. A list that changes order binds the items again in the same cells, so the cells do not fly; use a GridPanel to animate a reorder.

view.AnimateReflow = true;
view.Reflow.Stiffness = 300;
view.ReflowCompleted += () => Debug.Log("settled");
Member Description
AnimateReflow (animate-reflow) True to spring the cells to their new places. False by default.
Reflow The Reflow helper while AnimateReflow is on, otherwise null.
ReflowCompleted After every spring came to rest.

Styling

Class Element
tb-grid-view The grid (grows; background, border). With --paged. The border shows the accent color while it has the focus.
tb-grid-view__scroll, __content The scroll view and its content. The grid reads the padding of __content and places the cells inside it.
tb-grid-view__item The cell that holds the element of MakeItem, with --selected (an accent ring) and --current (the item the keys move from).
tb-grid-view__label The label of a cell without MakeItem.
tb-grid-view__pagination The page buttons.

Style the element of a selected item through the cell:

.tb-grid-view__item--selected > .card {
    background-color: var(--tb-color-accent);
}

The GridViewDemo scene shows a gallery of 2000 icons, a card collection with a multiple selection, paged achievements that load with skeletons, and the cell sizes.