Class BaseGridView
The base class for GridView elements.
Implements
Inherited Members
Namespace: Unity.AppUI.UI
Assembly: Unity.AppUI.dll
Syntax
public abstract class BaseGridView : BindableElement, IEventHandler, ITransform, ITransitionAnimations, IExperimentalFeatures, IVisualElementScheduler, IResolvedStyle, IBindable, ISerializationCallbackReceiver
Constructors
BaseGridView()
Creates a BaseGridView with all default properties.
Declaration
protected BaseGridView()
BaseGridView(IList, Func<VisualElement>, Action<VisualElement, int>)
Constructs a BaseGridView, with all required properties provided.
Declaration
public BaseGridView(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
firstColumnUssClassName
The first column USS class name for GridView elements.
Declaration
public static readonly string firstColumnUssClassName
Field Value
Type | Description |
---|---|
string |
itemUssClassName
The USS class name of item elements in GridView elements.
Declaration
public static readonly string itemUssClassName
Field Value
Type | Description |
---|---|
string |
Remarks
Unity adds this USS class to every item element the GridView contains. Any styling applied to this class affects every item element located beside, or below the stylesheet in the visual tree.
lastColumnUssClassName
The last column USS class name for GridView elements.
Declaration
public static readonly string lastColumnUssClassName
Field Value
Type | Description |
---|---|
string |
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 |
itemWidth
The width of the BaseGridView items.
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 |
resolvedItemWidth
The width of the BaseGridView items in pixels.
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> |
selectionCount
The number of selected Items.
Declaration
public int selectionCount { get; }
Property Value
Type | Description |
---|---|
int |
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.
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. |
ApplySelectedState()
Set the selected visual state on items.
Declaration
protected abstract void ApplySelectedState()
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()
GetIdFromIndex(int)
Returns the index of the item at the given position.
Declaration
protected int GetIdFromIndex(int index)
Parameters
Type | Name | Description |
---|---|---|
int | index | The index of the item to get the ID from. |
Returns
Type | Description |
---|---|
int | The ID of the item at the given index. |
GetIndexByWorldPosition(Vector2)
Returns the index of the item at the given position.
Declaration
public abstract 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.
GetVisualElementInternal(int)
Implement this method to return the VisualElement at the specified index.
Declaration
protected abstract VisualElement GetVisualElementInternal(int index)
Parameters
Type | Name | Description |
---|---|---|
int | index | The index of the VisualElement to return. |
Returns
Type | Description |
---|---|
VisualElement | The VisualElement at the specified index. |
HasValidDataAndBindings()
Whether the GridView has valid data and bindings.
Declaration
protected bool HasValidDataAndBindings()
Returns
Type | Description |
---|---|
bool | True if the GridView has valid data and bindings, false otherwise. |
IsSelectedId(int)
Check if the item with the given id is selected.
Declaration
public bool IsSelectedId(int id)
Parameters
Type | Name | Description |
---|---|---|
int | id | The id of the item to check. |
Returns
Type | Description |
---|---|
bool | True if the item is selected, false otherwise. |
OnContainerHeightChanged(float)
Callback called when the ScrollView container height changes.
Declaration
protected abstract void OnContainerHeightChanged(float height)
Parameters
Type | Name | Description |
---|---|---|
float | height | The new height of the container. |
OnCustomStyleResolved(CustomStyleResolvedEvent)
Method to implement to handle the custom style resolved event.
Declaration
protected abstract void OnCustomStyleResolved(CustomStyleResolvedEvent e)
Parameters
Type | Name | Description |
---|---|---|
CustomStyleResolvedEvent | e | The custom style resolved event. |
OnScroll(float)
Callback called when the scroll value changes.
Declaration
protected abstract void OnScroll(float offset)
Parameters
Type | Name | Description |
---|---|---|
float | offset | The new scroll offset. |
Remarks
You can also call this method one frame your Refresh() implementation to ensure the visual state of the grid is updated correctly.
PostSelection(bool, bool)
Method to call after the selection has been changed to update the selection state and send the selection changed event.
Declaration
protected void PostSelection(bool updatePreviousSelection, bool sendNotification)
Parameters
Type | Name | Description |
---|---|---|
bool | updatePreviousSelection | Whether to update the previous selection. |
bool | sendNotification | Whether to send the selection changed event. |
Refresh()
Clears the GridView, recreates all visible visual elements, and rebinds all items.
Declaration
public virtual 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 abstract 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.