Class TreeList
A container that turns a hierarchy of TreeViewItem elements into a single navigable, selectable tree.
Implements
Inherited Members
Namespace: Unity.AppUI.UI
Assembly: Unity.AppUI.dll
Syntax
[UxmlElement]
public class TreeList : BaseVisualElement, IEventHandler, IResolvedStyle, ITransform, ITransitionAnimations, IExperimentalFeatures, IVisualElementScheduler, IContextOverrideElement, IAdditionalDataHolder, ISizeableElement
Remarks
TreeList is a composition container, not a data-source collection view: you build the
hierarchy by nesting TreeViewItem elements, exactly the way the App UI web
registry composes <TreeList> and <TreeViewItem>. The container owns
everything that is a property of the tree as a whole — selection, keyboard navigation, caret
clicks and sizing — while each item owns its own label, caret and expansion state.
This deliberately does not wrap TreeView. App UI
answers this question the same way for every collection surface it ships: see
BaseGridView, which is written from scratch on top of BindableElement
rather than deriving from BaseVerticalCollectionView, and borrows only the
SelectionType vocabulary from UI Toolkit. The built-in TreeView is a virtualised,
data-bound view built around TreeViewItemData and its own item templates, which cannot
express the registry's composition API or App UI's styling contract. Following the established
pattern keeps the App UI collection family consistent. It is named TreeList, not TreeView, so
it never shadows the UI Toolkit type in a file that imports both namespaces.
Selection semantics follow SelectionType:
- None — the tree never selects anything.
- Single — one item at a time; modifiers are ignored.
- Multiple — the action key (Cmd on macOS, Ctrl elsewhere) toggles an item, Shift extends a range from the anchor over the visibleItems order, and action+Shift unions the range with the current selection. A modified press applies at once; a plain press changes nothing until the release, and only a release that ends a click (the pointer travelled less than 4px) selects the row. A press that becomes a drag leaves the selection as it was, so a group stays whole for a group drag.
Three behaviours are opt-in through a handler, and are completely inert until one is set, the same way the registry gates them:
- onReparent turns on drag-to-reorder. The tree resolves the drop target and its DropPosition, paints the indicators and refuses illegal drops, then hands the move to the handler — it never reparents anything itself.
- onDeleteSelection handles Delete and Backspace. There is no default removal.
Examples
<UXML xmlns:ui="Unity.AppUI.UI">
<ui:TreeList size="Md" selection-type="Multiple">
<ui:TreeViewItem label="Assets" icon="folder" show-caret="true" expanded="true">
<ui:TreeViewItem label="Scenes" icon="folder" depth="1" />
<ui:TreeViewItem label="Scripts" icon="folder" depth="1" draggable="false" />
</ui:TreeViewItem>
</ui:TreeList>
</UXML>
tree.onReparent = (dragged, target, position) => model.Move(dragged, target, position);
tree.onDeleteSelection = selection => model.Delete(selection);
Constructors
TreeList()
Initializes a new instance of the TreeList class.
Declaration
public TreeList()
Fields
draggingUssClassName
The USS class name applied to the tree while a row drag is in progress.
Declaration
public const string draggingUssClassName = "appui-tree-list--dragging"
Field Value
| Type | Description |
|---|---|
| string |
sizeUssClassName
The TreeList's size USS class name. The container carries the size and the stylesheet cascades it onto every TreeViewItem row it contains.
Declaration
public const string sizeUssClassName = "appui-tree-list--size-"
Field Value
| Type | Description |
|---|---|
| string |
treeItemMimeType
The MIME type a tree-row drag publishes itself under. See TreeListDragPayload.
Declaration
public const string treeItemMimeType = "application/x-appui-tree-item"
Field Value
| Type | Description |
|---|---|
| string |
ussClassName
The TreeList's main USS class name.
Declaration
public const string ussClassName = "appui-tree-list"
Field Value
| Type | Description |
|---|---|
| string |
Properties
allowNoSelection
Whether the TreeList is allowed to have an empty selection. When false, a request that would empty the selection is ignored, and setting it to false selects the first visible item if nothing is selected yet.
Declaration
[CreateProperty]
[UxmlAttribute]
public bool allowNoSelection { get; set; }
Property Value
| Type | Description |
|---|---|
| bool |
caretIcon
Gets or sets the disclosure glyph every row in this tree draws, by icon name. Null or empty is the default caret.
Declaration
[CreateProperty]
[UxmlAttribute]
public string caretIcon { get; set; }
Property Value
| Type | Description |
|---|---|
| string |
Remarks
The glyph rotates with the row's expansion either way, and the caret gutter keeps its width, so this is a glyph swap inside the existing row anatomy: a denser tree can ask for a 16-grid chevron in place of the 32-grid caret. The tree hands the name down as a context rather than walking its rows, so a row added later picks it up as well.
dragPayload
The payload of the drag in progress, or null. See TreeListDragPayload.
Declaration
public TreeListDragPayload dragPayload { get; }
Property Value
| Type | Description |
|---|---|
| TreeListDragPayload |
draggedItem
The row currently being dragged, or null when no drag is in progress.
Declaration
public TreeViewItem draggedItem { get; }
Property Value
| Type | Description |
|---|---|
| TreeViewItem |
dragger
The Dragger manipulator that turns a press on a row into a reorder drag.
Declaration
public Dragger dragger { get; }
Property Value
| Type | Description |
|---|---|
| Dragger |
dropPosition
Where the dragged row would land relative to dropTargetItem, or null when there is no valid target under the pointer.
Declaration
public DropPosition? dropPosition { get; }
Property Value
| Type | Description |
|---|---|
| DropPosition? |
dropTargetItem
The row a drag is currently hovering as its drop target, or null.
Declaration
public TreeViewItem dropTargetItem { get; }
Property Value
| Type | Description |
|---|---|
| TreeViewItem |
items
Every TreeViewItem in this tree, in document order, whether or not it is currently reachable on screen.
Declaration
public IReadOnlyList<TreeViewItem> items { get; }
Property Value
| Type | Description |
|---|---|
| IReadOnlyList<TreeViewItem> |
keyboardNavigation
Whether the tree takes keyboard focus and navigates its rows with the arrow keys, Home / End, action+A, Escape and Delete. True by default.
Declaration
[CreateProperty]
[UxmlAttribute]
public bool keyboardNavigation { get; set; }
Property Value
| Type | Description |
|---|---|
| bool |
Remarks
Turn it off when the tree only provides selection and caret handling for rows another element
lays out, a timeline's gutter say, and that element owns the keyboard: the tree then neither
takes focus nor swallows keys bubbling up from the host. This is the UITK counterpart of the
web registry's TreeViewProvider. Each expandable row then opens and closes itself on
Right and Left (see showCaret).
Leave onReparent unset for rows laid out as siblings: a drop on a descendant is told from element nesting, which flat rows do not have.
onDeleteSelection
Handler called when Delete or Backspace is pressed with a non-empty selection, receiving that selection in priority order.
Declaration
public Action<IEnumerable<TreeViewItem>> onDeleteSelection { get; set; }
Property Value
| Type | Description |
|---|---|
| Action<IEnumerable<TreeViewItem>> |
Remarks
There is no default behaviour: the tree never removes a row on its own, because a row is usually a projection of an external model that has to be the one to delete. With no handler the keys are simply not handled.
onReparent
Handler called on a successful drop, with the dragged row, the row it was dropped on, and where it landed relative to that row.
Declaration
public Action<TreeViewItem, TreeViewItem, DropPosition> onReparent { get; set; }
Property Value
| Type | Description |
|---|---|
| Action<TreeViewItem, TreeViewItem, DropPosition> |
Remarks
Setting this is what turns drag-to-reorder on: with no handler the tree never starts a row
drag at all, exactly like the registry, where the whole DnD surface is gated on
onReparent. The tree deliberately does not move any element itself — reparenting
is the owner's job, because the rows are usually a projection of an external model.
Individual rows opt out of being picked up with draggable.
selectedItem
The primary selected item — the one the user interacted with last — or null when nothing is selected. Keyboard navigation moves relative to this item.
Declaration
[CreateProperty(ReadOnly = true)]
public TreeViewItem selectedItem { get; }
Property Value
| Type | Description |
|---|---|
| TreeViewItem |
selectedItems
The current selection, in priority order. Index 0 is the primary — see selectedItem.
Declaration
[CreateProperty(ReadOnly = true)]
public IReadOnlyList<TreeViewItem> selectedItems { get; }
Property Value
| Type | Description |
|---|---|
| IReadOnlyList<TreeViewItem> |
selectionType
How many items the user can select at once. Defaults to Single.
Declaration
[CreateProperty]
[UxmlAttribute]
public SelectionType selectionType { get; set; }
Property Value
| Type | Description |
|---|---|
| SelectionType |
Remarks
Narrowing the selection type trims the current selection: None clears it and Single keeps only the primary.
size
The size of the rows in this TreeList.
Declaration
[CreateProperty]
[UxmlAttribute]
public Size size { get; set; }
Property Value
| Type | Description |
|---|---|
| Size |
Remarks
The container carries the size class and TreeList.uss cascades the matching
control-sizing alias onto the headers of the TreeViewItem elements inside it,
so items added later pick the size up with no extra bookkeeping.
visibleItems
The items the user can currently see, in top-to-bottom order: document order minus every item that sits inside a collapsed ancestor. This is the order Shift-range selection, SelectAll() and arrow navigation work over.
Declaration
public IReadOnlyList<TreeViewItem> visibleItems { get; }
Property Value
| Type | Description |
|---|---|
| IReadOnlyList<TreeViewItem> |
Methods
BeginDrag(TreeViewItem)
Starts a reorder drag for item. A no-op when onReparent is
unset or the item is not draggable.
Declaration
public void BeginDrag(TreeViewItem item)
Parameters
| Type | Name | Description |
|---|---|---|
| TreeViewItem | item | The row to pick up. |
CanDrop(TreeViewItem, TreeViewItem)
Whether target can receive item.
Declaration
public bool CanDrop(TreeViewItem item, TreeViewItem target)
Parameters
| Type | Name | Description |
|---|---|---|
| TreeViewItem | item | The row being dragged. |
| TreeViewItem | target | The candidate drop target. |
Returns
| Type | Description |
|---|---|
| bool | True when the drop is legal. |
Remarks
A row cannot be dropped onto itself, nor onto any of its own descendants — that would detach the subtree from the tree.
CancelDrag()
Abandons the drag in progress without dropping.
Declaration
public void CancelDrag()
ClearSelection()
Clears the selection. Does nothing when allowNoSelection is false.
Declaration
public void ClearSelection()
CollapseAll()
Collapses every item that has children.
Declaration
public void CollapseAll()
EndDrag()
Ends the drag in progress, invoking onReparent when it ends over a valid drop target.
Declaration
public bool EndDrag()
Returns
| Type | Description |
|---|---|
| bool | True when a drop was performed. |
ExpandAll()
Expands every item that has children.
Declaration
public void ExpandAll()
GetDropPosition(TreeViewItem, Vector2)
The DropPosition a pointer at worldPosition reads as over
item: the top and bottom quarters of the row are Before and After, the
middle half is Inside.
Declaration
public DropPosition GetDropPosition(TreeViewItem item, Vector2 worldPosition)
Parameters
| Type | Name | Description |
|---|---|---|
| TreeViewItem | item | The row under the pointer. |
| Vector2 | worldPosition | The pointer position, in panel space. |
Returns
| Type | Description |
|---|---|
| DropPosition | The resolved drop position. |
GetItemAtPosition(Vector2)
The visible row whose header contains worldPosition, or null.
Declaration
public TreeViewItem GetItemAtPosition(Vector2 worldPosition)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector2 | worldPosition | A position in panel space. |
Returns
| Type | Description |
|---|---|
| TreeViewItem | The row under that position. |
GetSizeUssClassName(Size)
Declaration
public static string GetSizeUssClassName(Size enumValue)
Parameters
| Type | Name | Description |
|---|---|---|
| Size | enumValue |
Returns
| Type | Description |
|---|---|
| string |
Select(TreeViewItem, bool, bool)
Applies a selection interaction for an item, resolving the modifiers against the current selectionType. This is the entry point pointer and keyboard interactions go through, and it is public so callers can reproduce a click from code.
Declaration
public void Select(TreeViewItem item, bool shift = false, bool toggle = false)
Parameters
| Type | Name | Description |
|---|---|---|
| TreeViewItem | item | The item the user acted on. |
| bool | shift | True when the range modifier is held. Ignored unless the tree is in Multiple mode and an anchor exists. |
| bool | toggle | True when the action key (Cmd on macOS, Ctrl elsewhere) is held. Ignored unless the tree is in Multiple mode. |
SelectAll()
Selects every item in visibleItems. Only valid in Multiple mode; a no-op otherwise.
Declaration
public void SelectAll()
SetSelection(IEnumerable<TreeViewItem>)
Replaces the current selection, raising selectionChanged when it changes.
Declaration
public void SetSelection(IEnumerable<TreeViewItem> itemsToSelect)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<TreeViewItem> | itemsToSelect | The items to select, in priority order. In Single mode only the last one survives, matching how BaseGridView narrows a multi-index request. |
SetSelection(TreeViewItem)
Selects a single item, replacing the current selection.
Declaration
public void SetSelection(TreeViewItem item)
Parameters
| Type | Name | Description |
|---|---|---|
| TreeViewItem | item | The item to select, or null to clear the selection. |
SetSelectionWithoutNotify(IEnumerable<TreeViewItem>)
Replaces the current selection without raising selectionChanged.
Declaration
public void SetSelectionWithoutNotify(IEnumerable<TreeViewItem> itemsToSelect)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<TreeViewItem> | itemsToSelect | The items to select, in priority order. |
UpdateDrag(Vector2)
Re-evaluates the drop target for a pointer at worldPosition, updating the
drop indicators.
Declaration
public void UpdateDrag(Vector2 worldPosition)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector2 | worldPosition | The pointer position, in panel space. |
Events
selectionChanged
Event raised when selectedItems changes, with the new selection in priority order. Not raised by SetSelectionWithoutNotify(IEnumerable<TreeViewItem>), nor when a selection request resolves to the selection that was already in place.
Declaration
public event Action<IEnumerable<TreeViewItem>> selectionChanged
Event Type
| Type | Description |
|---|---|
| Action<IEnumerable<TreeViewItem>> |