UI Toolbox 徽标UI Toolbox
支持

本页为译文。技术支持和视频教程主要使用英语,内容以英文页面为准。 英文版

DataGrid

UIToolbox 中的 DataGrid 以表格形式显示条目列表,并带有带类型的单元格编辑器。它封装了原生的 MultiColumnListView,所以行是虚拟的:只有可见的行才有元素。

英雄表格,含名称、职业、等级、伤害和启用列,按等级排序,选中一行英雄表格,含名称、职业、等级、伤害和启用列,按等级排序,选中一行

视频教程:Data grid。所有视频:视频教程。

<tb:DataGrid
    name="units"
    selection-mode="Multiple"
    style="height: 250px;">
    <tb:DataGridColumn header="Name" binding-path="Name" width="130" />
    <tb:DataGridColumn header="Level" binding-path="Level" width="70" />
    <tb:DataGridColumn header="Class" binding-path="Class" width="100" />
    <tb:DataGridColumn
        header="Action"
        cell-type="Button"
        button-text="Heal"
        width="70"
        sortable="false" />
</tb:DataGrid>
var grid = root.Q<DataGrid>("units");
grid.ItemsSource = units;   // a List<Unit>; the edits write to the items
grid.CellValueChanged += (i, column, old, now) =>
    Debug.Log($"{units[i].Name}: {column.Header} {old} to {now}");
grid.CellClicked += (i, column) =>
    grid.SetValue(i, grid.GetColumn("Health")!, 100f);
grid.SetFilter("Level", level => (int)level! >= 10);
grid.SearchText = "ash";
GUIUtility.systemCopyBuffer = grid.ToCsv(selectedOnly: true);

带复选框列、每页 25 行并带翻页按钮、以及页脚行的表格:

<tb:DataGrid
    name="units"
    selection-column="true"
    page-size="25"
    show-pagination="true"
    show-footer="true">
    <tb:DataGridColumn
        header="Name"
        binding-path="Name"
        width="130"
        can-hide="false"
        footer-text="Total" />
    <tb:DataGridColumn
        header="Level"
        binding-path="Level"
        width="70"
        footer-text="Avg " />
    <tb:DataGridColumn
        header="Notes"
        binding-path="Notes"
        width="200"
        visible="false" />
</tb:DataGrid>
grid.SetFooter("Level", rows =>
    rows.Count == 0 ? ""
        : rows.Average(i => units[i].Level).ToString("F1"));
columnsButton.clicked += () =>
    grid.ShowColumnMenu(
        new Vector2(columnsButton.worldBound.xMin,
            columnsButton.worldBound.yMax));
grid.VisibleColumnsChanged += () =>
    PlayerPrefs.SetString("columns",
        string.Join(",", grid.VisibleColumns));
grid.SetVisibleColumns(
    PlayerPrefs.GetString("columns", "Name,Level").Split(','));
成员 说明
DataGridColumn.Header (header), BindingPath (binding-path), Width (width, 100), Sortable (sortable, true) 表头文字、条目的公共字段或属性(无值的列为空)、起始宽度,以及点击表头排序。
DataGridColumn.CellType (cell-type) DataGridCellType:Auto(默认)按成员类型选择编辑器:bool 用 Toggle,int 和 long 用 Integer,float、double 和 decimal 用 Float,枚举或带 Choices 的字符串用 Dropdown,字符串用 Text,float[]、List<float> 或其他浮点数列表用 Sparkline,其他类型用 Label。Button 发送 CellClicked。Sparkline 单元格绘制这些值的 Sparkline,且从不编辑。
DataGridColumn.ReadOnly (read-only), FormatString (format-string), Choices (choices), ButtonText (button-text), SparklineType (sparkline-type) 用标签代替编辑器;标签的数字或日期格式;字符串下拉菜单的逗号分隔列表;按钮文字(为空则显示值);Sparkline 单元格的 Line(默认)、Bar 或 WinLoss。
DataGridColumn.Visible (visible, true), CanHide (can-hide, true) 列是否显示。CanHide 关闭的列不在列菜单中。
DataGridColumn.FooterText (footer-text) 该列页脚单元格的文字。请在 ShowFooter 之前设置,或调用 Refresh()。
DataGridColumn.Name, Member, ValueType, CanWrite, ResolvedCellType, FormatValue 绑定路径或表头、表格有了条目类型之后的成员,以及正在使用的编辑器。没有公共设置器的成员会得到标签。
ItemsSource, ItemType, Refresh 条目(一个 IList)。结构体条目会被写回列表。代码添加、移除或更改条目之后请调用 Refresh()。
Columns, AddColumn, RemoveColumn, GetColumn, AutoColumns (auto-columns, true) 按声明顺序排列的列。没有列的表格会为每个公共字段和属性添加一列。另一种类型的条目会替换这些列,但不会替换由代码或 UXML 添加的列。
SetCellRenderer(column, (index, item) => element) 用你的代码构建某一列的单元格。表格每次绑定一行时都会调用它。
GetValue, SetValue 按源索引读写单元格。SetValue 会把文字和数字转换为成员类型,无法转换或设置器抛出异常时返回 false(表格会记录该异常)。
ReadOnly (read-only) 所有单元格显示标签。开关被禁用。按钮仍然有效。
AllowSorting (allow-sorting), SortByColumn, ClearSort 点击表头排序;Shift+点击把一列加入排序。排序是稳定的。空值排在最前,数字和日期按值排序,浮点数列表按最后一个值排序,文字不分大小写按 A 到 Z 排序。
SetFilter, ClearFilter, ClearFilters, SearchText, ShownIndices 每列一个过滤器,以及一个搜索:显示单元格文字中不分大小写包含该文字的行。搜索只读取显示的列。ShownIndices 按显示顺序给出所显示行的源索引。
SelectionMode (selection-mode, Multiple), SelectedIndices, SelectedItems, SetSelection, ClearSelection, SelectionChanged 按源索引的选择。排序之后它保持在同样的条目上。隐藏了某个选中行的过滤器会把它从选择中移除。
ItemsChosen 对行的双击或 Enter,带有选中的条目。
CellValueChanged, CellClicked 改变了值的编辑或 SetValue;按钮点击。两者都给出源索引和列。
AllowColumnResize (allow-column-resize), AllowColumnReorder (allow-column-reorder), RowHeight (row-height, 24) 拖动表头边缘、拖动表头,以及固定的行高。
GroupBy (group-by), Groups, SetGroupExpanded, IsGroupExpanded, SetAllGroupsExpanded 按某一列的值对显示的行分组,每组一个表头行(“Class: Mage (3)”)。各组按值排序,并跟随该列排序的方向。组内的行保持排序。Groups 按显示顺序给出各值的表头文字。
AllowColumnHide (allow-column-hide, true), ShowColumnMenu(position) 在表头上右键点击会显示列菜单:每列一个勾选项。ShowColumnMenu 在面板位置显示它,供你自己的按钮使用。
VisibleColumns, SetVisibleColumns(names), VisibleColumnsChanged 显示的列的名称,按显示顺序。SetVisibleColumns 显示这些列并隐藏其他列。该事件在每次列显示或隐藏之后触发。
SelectionColumn (selection-column) 添加第一列,每行一个复选框,表头中有一个复选框对应所有显示的行。
PageSize (page-size, 0), Page, PageCount, PageChanged 一页的行数;0 显示所有行。Page 从 1 开始,并被限制在 PageCount 之内。PageChanged 给出新的页。
ShowPagination (show-pagination), Pagination PageSize 大于 0 时在表格下方显示翻页按钮。Pagination 是那个 Pagination 控件,用于它自己的设置,例如 SiblingCount。
FilteredIndices 所有页的行的源索引,按显示顺序。没有分页时与 ShownIndices 相同。
ShowFooter (show-footer), SetFooter(column, rows => text) 显示页脚行,每列一个单元格。单元格先显示 FooterText,然后是回调的文字。回调获得 FilteredIndices。Null 则移除。
ToCsv(selectedOnly) 显示的或选中的行的 RFC 4180 CSV,按显示的行和列顺序,数字使用不变区域设置。浮点数列表是一个单元格,例如 "3,5,4"。有分页时,它包含所有页的行。没有成员的列、隐藏的列和分组表头会被省略。
View 原生的 MultiColumnListView,用于表格没有封装的设置。

列菜单

选择列

分页

页脚

提示:

在示例中

样式

类 元素
tb-data-grid 表格(会伸展;表面背景、边框)。使用 ReadOnly 时会添加 tb-data-grid--read-only。
tb-data-grid__view 原生的 MultiColumnListView。表格给它的表头(surface-alt、粗体文字)、行(surface,奇数行 surface-alt,指针下 surface-hover,选中时强调色)和排序箭头着色。
tb-data-grid__cell 单元格。它容纳一个编辑器、标签或按钮,没有字段边距。编辑器在指针或焦点到来之前是扁平的。
tb-data-grid__cell-label 标签单元格的文字(省略号)。
tb-data-grid__group 分组表头行的标签。它填满该行:--tb-color-surface-alt 背景、粗体文字、悬停时 --tb-color-surface-hover。
tb-data-grid__select, tb-data-grid__select-all 选择列中行和表头的复选框:没有边距的原生 Toggle 元素。
tb-data-grid__footer 页脚:--tb-color-surface-alt 背景和顶部边框。它裁剪它的行。
tb-data-grid__footer-row 页脚单元格所在的行(高度至少 24 px)。
tb-data-grid__footer-cell 页脚单元格,一个 Label:粗体文字、省略号。表格设置它的宽度。
tb-data-grid__pagination 内置的 Pagination:居中,带顶部边框。

要把翻页按钮放到右边,并让页脚文字变成正常字体:

.tb-data-grid__pagination { justify-content: flex-end; }
.tb-data-grid__footer-cell { -unity-font-style: normal; }