Class ListBox
A vertical, virtualized, single-column collection of ListViewItem elements.
Inheritance
Implements
Inherited Members
Namespace: Unity.AppUI.UI
Assembly: Unity.AppUI.dll
Syntax
[UxmlElement]
public class ListBox : GridView, IEventHandler, IResolvedStyle, ITransform, ITransitionAnimations, IExperimentalFeatures, IVisualElementScheduler, IBindable, ISerializationCallbackReceiver
Remarks
ListBox is the container that ListViewItem lives in. It is a single-column GridView, so it inherits the whole App UI collection contract: an itemsSource data source, makeItem and bindItem recycling, row virtualization, a selection model (selectionType) and keyboard navigation.
This is deliberately App UI's own implementation rather than a wrapper around ListView, exactly as BaseGridView and GridView are. It keeps the App UI context system (theme, direction, density, SizeContext), the App UI styling vocabulary and the App UI selection semantics in one place, instead of straddling two collection frameworks.
Note: like GridView, the row height is fixed and must agree with the height
your items actually render at, because virtualization computes the scroll extent from it. Set
itemHeight, or drive it from USS with --unity-item-height.
If you do not provide makeItem and bindItem, ListBox builds a ListViewItem per row and puts the data item's string representation in its title.
Examples
Basic usage — a list of strings with the default item and single selection.
var list = new ListBox
{
itemsSource = new List<string> { "Materials", "Textures", "Prefabs" },
itemHeight = 40,
size = Size.Md,
};
list.selectionChanged += items => Debug.Log($"Selected: {string.Join(", ", items)}");
Custom items — supplying your own ListViewItem factory and binder.
var list = new ListBox
{
itemsSource = assets,
itemHeight = 56,
selectionType = SelectionType.Multiple,
makeItem = () => new ListViewItem(),
bindItem = (element, index) =>
{
var asset = (Asset)assets[index];
var item = (ListViewItem)element;
item.title = asset.name;
item.subtitle = asset.path;
item.thumbnail = asset.preview;
},
};
Using ListBox in a UXML template — declaring a ListBox in UXML.
<UXML xmlns:ui="Unity.AppUI.UI">
<ui:ListBox
size="Md"
item-height="40"
selection-type="Single"
allow-no-selection="true" />
</UXML>
Constructors
ListBox()
Creates a ListBox with all default properties.
Declaration
public ListBox()
ListBox(IList, Func<VisualElement>, Action<VisualElement, int>)
Constructs a ListBox, with all required properties provided.
Declaration
public ListBox(IList itemsSource, Func<VisualElement> makeItem, Action<VisualElement, int> bindItem)
Parameters
| Type | Name | Description |
|---|---|---|
| IList | itemsSource | The list of items to use as a data source. |
| Func<VisualElement> | makeItem | The factory method to call to create a display item. The method should return a VisualElement that can be bound to a data item. |
| Action<VisualElement, int> | bindItem | The method to call to bind a data item to a display item. The method receives as parameters the display item to bind, and the index of the data item to bind it to. |
Fields
sizeUssClassName
The ListBox's size USS class name prefix.
Declaration
public const string sizeUssClassName = "appui-list-box--size-"
Field Value
| Type | Description |
|---|---|
| string |
ussClassName
The ListBox's main USS class name.
Declaration
public const string ussClassName = "appui-list-box"
Field Value
| Type | Description |
|---|---|
| string |
Remarks
This class is added on top of ussClassName, so the shared grid rules keep applying.
Properties
size
The size of the ListBox's rows.
Declaration
[CreateProperty]
[UxmlAttribute]
public Size size { get; set; }
Property Value
| Type | Description |
|---|---|
| Size |
Remarks
The size is published to the subtree through SizeContext, so every ListViewItem the ListBox realizes adopts it, the same way AvatarGroup sizes its avatars. Setting size on an individual item after the fact still wins for that item.
Methods
GetSizeUssClassName(Size)
Declaration
public static string GetSizeUssClassName(Size enumValue)
Parameters
| Type | Name | Description |
|---|---|---|
| Size | enumValue |
Returns
| Type | Description |
|---|---|
| string |