Class GridView
A view containing recycled rows with items inside.
Implements
Inherited Members
Namespace: Unity.AppUI.UI
Assembly: Unity.AppUI.dll
Syntax
public class GridView : BindableElement, IEventHandler, ITransform, ITransitionAnimations, IExperimentalFeatures, IVisualElementScheduler, IResolvedStyle, IBindable, ISerializationCallbackReceiver
Constructors
GridView()
Creates a GridView with all default properties. The itemsSource, itemHeight, makeItem and bindItem properties must all be set for the GridView to function properly.
Declaration
public GridView()
GridView(IList, Func<VisualElement>, Action<VisualElement, int>)
Constructs a GridView, with all required properties provided.
Declaration
public GridView(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
ussClassName
The USS class name for GridView elements.
Declaration
public const string ussClassName = "appui-grid-view"
Field Value
Type | Description |
---|---|
string |
Remarks
Unity adds this USS class to every instance of the GridView element. Any styling applied to this class affects every GridView located beside, or below the stylesheet in the visual tree.
Properties
allowNoSelection
Declaration
public bool allowNoSelection { get; set; }
Property Value
Type | Description |
---|---|
bool |
bindItem
Callback for binding a data item to the visual element.
Declaration
public Action<VisualElement, int> bindItem { get; set; }
Property Value
Type | Description |
---|---|
Action<VisualElement, int> |
Remarks
The method called by this callback receives the VisualElement to bind, and the index of the element to bind it to.
columnCount
The number of columns for this grid.
Declaration
public int columnCount { get; set; }
Property Value
Type | Description |
---|---|
int |
contentContainer
Returns the content container for the GridView. Because the GridView control automatically manages its content, this always returns null.
Declaration
public override VisualElement contentContainer { get; }
Property Value
Type | Description |
---|---|
VisualElement |
Overrides
dragger
Declaration
public Dragger dragger { get; }
Property Value
Type | Description |
---|---|
Dragger |
getItemId
Callback for getting the ID of an item.
Declaration
public Func<int, int> getItemId { get; set; }
Property Value
Type | Description |
---|---|
Func<int, int> |
Remarks
The method called by this callback receives the index of the item to get the ID from.
isSelecting
Returns true if the soft-selection is in progress.
Declaration
public bool isSelecting { get; }
Property Value
Type | Description |
---|---|
bool |
itemHeight
The height of a single item in the list, in pixels.
Declaration
public int itemHeight { get; set; }
Property Value
Type | Description |
---|---|
int |
Remarks
GridView requires that all visual elements have the same height so that it can calculate the scroller size.
This property must be set for the list view to function.
itemWidth
Declaration
public float itemWidth { get; }
Property Value
Type | Description |
---|---|
float |
itemsSource
The data source for list items.
Declaration
public IList itemsSource { get; set; }
Property Value
Type | Description |
---|---|
IList |
Remarks
This list contains the items that the GridView displays.
This property must be set for the list view to function.
makeItem
Callback for constructing the VisualElement that is the template for each recycled and re-bound element in the list.
Declaration
public Func<VisualElement> makeItem { get; set; }
Property Value
Type | Description |
---|---|
Func<VisualElement> |
Remarks
This callback needs to call a function that constructs a blank VisualElement that is bound to an element from the list.
The GridView automatically creates enough elements to fill the visible area, and adds more if the area is expanded. As the user scrolls, the GridView cycles elements in and out as they appear or disappear.
This property must be set for the list view to function.
operationMask
A mask describing available operations in this GridView when the user interacts with it.
Declaration
public GridView.GridOperations operationMask { get; set; }
Property Value
Type | Description |
---|---|
GridView.GridOperations |
preventScrollWithModifiers
Prevents the grid view from scrolling when the user presses a modifier key at the same time as scrolling.
Declaration
public bool preventScrollWithModifiers { get; set; }
Property Value
Type | Description |
---|---|
bool |
resolvedItemHeight
The computed pixel-aligned height for the list elements.
Declaration
public float resolvedItemHeight { get; }
Property Value
Type | Description |
---|---|
float |
Remarks
This value changes depending on the current panel's DPI scaling.
See Also
resolvedItemWidth
Declaration
public float resolvedItemWidth { get; }
Property Value
Type | Description |
---|---|
float |
scrollView
The ScrollView used by the GridView.
Declaration
public ScrollView scrollView { get; }
Property Value
Type | Description |
---|---|
ScrollView |
selectedIds
Returns the IDs of selected items in the data source. Always returns an enumerable, even if no item is selected, or a single item is selected.
Declaration
public IEnumerable<int> selectedIds { get; }
Property Value
Type | Description |
---|---|
IEnumerable<int> |
selectedIndex
Returns or sets the selected item's index in the data source. If multiple items are selected, returns the first selected item's index. If multiple items are provided, sets them all as selected.
Declaration
public int selectedIndex { get; set; }
Property Value
Type | Description |
---|---|
int |
selectedIndices
Returns the indices of selected items in the data source. Always returns an enumerable, even if no item is selected, or a single item is selected.
Declaration
public IEnumerable<int> selectedIndices { get; }
Property Value
Type | Description |
---|---|
IEnumerable<int> |
selectedItem
Returns the selected item from the data source. If multiple items are selected, returns the first selected item.
Declaration
public object selectedItem { get; }
Property Value
Type | Description |
---|---|
object |
selectedItems
Returns the selected items from the data source. Always returns an enumerable, even if no item is selected, or a single item is selected.
Declaration
public IEnumerable<object> selectedItems { get; }
Property Value
Type | Description |
---|---|
IEnumerable<object> |
selectionType
Controls the selection type.
Declaration
public SelectionType selectionType { get; set; }
Property Value
Type | Description |
---|---|
SelectionType |
Remarks
You can set the GridView to make one item selectable at a time, make multiple items selectable, or disable selections completely.
When you set the GridView to disable selections, any current selection is cleared.
showBorder
Enable this property to display a border around the GridView.
Declaration
public bool showBorder { get; set; }
Property Value
Type | Description |
---|---|
bool |
Remarks
If set to true, a border appears around the ScrollView.
unbindItem
Callback for unbinding a data item from the VisualElement.
Declaration
public Action<VisualElement, int> unbindItem { get; set; }
Property Value
Type | Description |
---|---|
Action<VisualElement, int> |
Remarks
The method called by this callback receives the VisualElement to unbind, and the index of the element to unbind it from.
Methods
AddToSelection(int)
Adds an item to the collection of selected items.
Declaration
public void AddToSelection(int index)
Parameters
Type | Name | Description |
---|---|---|
int | index | Item index. |
CancelDrag()
Cancel drag operation.
Declaration
public void CancelDrag()
ClearSelection()
Deselects any selected items.
Declaration
public void ClearSelection()
ClearSelectionWithoutNotify()
Clear the selection without triggering selection changed event.
Declaration
public void ClearSelectionWithoutNotify()
GetIndexByWorldPosition(Vector2)
Returns the index of the item at the given position.
Declaration
public int GetIndexByWorldPosition(Vector2 worldPosition)
Parameters
Type | Name | Description |
---|---|---|
Vector2 | worldPosition | The position of the item in the world-space. |
Returns
Type | Description |
---|---|
int | The index of the item at the given position. |
Remarks
The position is relative to the top left corner of the grid. No check is made to see if the index is valid.
Refresh()
Clears the GridView, recreates all visible visual elements, and rebinds all items.
Declaration
public void Refresh()
Remarks
Call this method whenever the data source changes.
RemoveFromSelection(int)
Removes an item from the collection of selected items.
Declaration
public void RemoveFromSelection(int index)
Parameters
Type | Name | Description |
---|---|---|
int | index | The item index. |
ScrollToItem(int)
Scrolls to a specific item index and makes it visible.
Declaration
public void ScrollToItem(int index)
Parameters
Type | Name | Description |
---|---|---|
int | index | Item index to scroll to. Specify -1 to make the last item visible. |
SetSelection(IEnumerable<int>)
Sets a collection of selected items.
Declaration
public void SetSelection(IEnumerable<int> indices)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<int> | indices | The collection of the indices of the items to be selected. |
SetSelection(int)
Sets the currently selected item.
Declaration
public void SetSelection(int index)
Parameters
Type | Name | Description |
---|---|---|
int | index | The item index. |
SetSelectionWithoutNotify(IEnumerable<int>)
Sets a collection of selected items without triggering a selection change callback.
Declaration
public void SetSelectionWithoutNotify(IEnumerable<int> indices)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<int> | indices | The collection of items to be selected. |
Events
contextClicked
Callback triggered when the user right-clicks on an item.
Declaration
public event Action<PointerDownEvent> contextClicked
Event Type
Type | Description |
---|---|
Action<PointerDownEvent> |
Remarks
This callback receives an enumerable that contains the item or items selected.
doubleClicked
Callback triggered when the user double-clicks on an item.
Declaration
public event Action<int> doubleClicked
Event Type
Type | Description |
---|---|
Action<int> |
dragCanceled
Callback triggered when drag has been canceled.
Declaration
public event Action dragCanceled
Event Type
Type | Description |
---|---|
Action |
dragFinished
Callback triggered when drag has finished.
Declaration
public event Action<PointerUpEvent> dragFinished
Event Type
Type | Description |
---|---|
Action<PointerUpEvent> |
dragStarted
Callback triggered when drag has started.
Declaration
public event Action<PointerMoveEvent> dragStarted
Event Type
Type | Description |
---|---|
Action<PointerMoveEvent> |
dragUpdated
Callback triggered when items are dragged.
Declaration
public event Action<PointerMoveEvent> dragUpdated
Event Type
Type | Description |
---|---|
Action<PointerMoveEvent> |
itemsChosen
Callback triggered when the user acts on a selection of one or more items, for example by double-clicking or pressing Enter.
Declaration
public event Action<IEnumerable<object>> itemsChosen
Event Type
Type | Description |
---|---|
Action<IEnumerable<object>> |
Remarks
This callback receives an enumerable that contains the item or items chosen.
selectedIndicesChanged
Callback triggered when the selection changes.
Declaration
public event Action<IEnumerable<int>> selectedIndicesChanged
Event Type
Type | Description |
---|---|
Action<IEnumerable<int>> |
Remarks
This callback receives an enumerable that contains the indices of selected items.
selectionChanged
Callback triggered when the selection changes.
Declaration
public event Action<IEnumerable<object>> selectionChanged
Event Type
Type | Description |
---|---|
Action<IEnumerable<object>> |
Remarks
This callback receives an enumerable that contains the item or items selected.