TreeDataGrid Class
Definition
Represents a control that displays hierarchical and tabular data together in a single view. It is a combination of a TreeView and DataGrid control.
public class TreeDataGrid
Remarks
The TreeDataGrid supports both flat (2D table) and hierarchical (tree with columns) layouts, making it suitable for a variety of data visualization needs.
Key features include:
- Supports both row selection and cell selection modes
- Column sorting capabilities
- Support for various column types (Text, CheckBox, Template)
- MVVM-first data model
- Drag and drop functionality for rows
Constructors
| Name | Description |
|---|---|
| TreeDataGrid | Initializes a new instance of the Avalonia.Controls.TreeDataGrid class. |
TreeDataGrid Constructor
Initializes a new instance of the Avalonia.Controls.TreeDataGrid class.
public TreeDataGrid()
Methods
| Name | Description |
|---|---|
| TryGetCell (2 overloads) | No summary available. |
| TryGetRow (2 overloads) | No summary available. |
| TryGetRowModel | No summary available. |
TryGetCell overloads
TryGetCell Method
public bool TryGetCell(Avalonia.Controls.Control element, Avalonia.Controls.Primitives.TreeDataGridCell& result)
Parameters
element Avalonia.Controls.Control
result Avalonia.Controls.Primitives.TreeDataGridCell&
Returns
bool
TryGetCell Method
Attempts to retrieve a realized cell at the specified column and row indices.
public Avalonia.Controls.Control TryGetCell(int columnIndex, int rowIndex)
Parameters
columnIndex int
The index of the column.
rowIndex int
The index of the row.
Returns
Avalonia.Controls.Control
The cell at the specified indices, or null if no cell exists at that position.
Remarks
The row and column indices are based upon the data currently displayed in the TreeDataGrid, and not the underlying data source. For example, if the data is sorted, filtered or contains expanded hierarchical rows, the indices may not correspond directly to the data source. In addition, if the specified cell is not currently visible (e.g. scrolled out of view), this method will return null.
TryGetRow overloads
TryGetRow Method
public bool TryGetRow(Avalonia.Controls.Control element, Avalonia.Controls.Primitives.TreeDataGridRow& result)
Parameters
element Avalonia.Controls.Control
result Avalonia.Controls.Primitives.TreeDataGridRow&
Returns
bool
TryGetRow Method
Attempts to retrieve a realized row at the specified index.
public Avalonia.Controls.Primitives.TreeDataGridRow TryGetRow(int rowIndex)
Parameters
rowIndex int
The index of the row.
Returns
Avalonia.Controls.Primitives.TreeDataGridRow
The row at the specified index, or null if no row exists at that position.
Remarks
The row index is based upon the data currently displayed in the TreeDataGrid, and not the underlying data source. For example, if the data is sorted or filtered, the indices may not correspond directly to the data source. In addition, if the specified cell is not currently visible (e.g. scrolled out of view), this method will return null.
TryGetRowModel Method
public bool TryGetRowModel<TModel>(Avalonia.Controls.Control element, TModel& result)
Parameters
element Avalonia.Controls.Control
result TModel&
Type Parameters
TModel
Returns
bool
Properties
| Name | Description |
|---|---|
| AllowTriStateSorting | Gets or sets a value indicating whether the user can cycle through ascending, descending, and unsorted states when clicking the column header. The default is false. |
| AutoDragDropRows | Gets or sets a value indicating whether rows can be automatically dragged and dropped within the TreeDataGrid. |
| CanUserResizeColumns | Gets or sets a value indicating whether users can resize columns. |
| CanUserSortColumns | Gets or sets a value indicating whether users can sort columns. |
| ColumnHeaderTheme | Gets or sets the theme applied to all column headers. |
| ColumnHeadersPresenter | Gets the presenter that displays the column headers. |
| Columns | Gets the columns defined on the Avalonia.Controls.TreeDataGrid. |
| ElementFactory | Gets the element factory used to create UI elements for the TreeDataGrid. |
| FrozenColumnCount | Gets or sets the number of columns that are frozen on the left side. |
| ItemsSource | Gets or sets the items source for the TreeDataGrid. |
| RowTheme | Gets or sets the theme applied to all rows. |
| Rows | Gets the rows collection of the TreeDataGrid. |
| RowsPresenter | Gets the presenter that displays the rows. |
| Scroll | Gets the scroll viewer that enables scrolling through the TreeDataGrid content. |
| Selection | Gets the selection model for the TreeDataGrid. |
| SelectionMode | Gets or sets the selection mode for the TreeDataGrid. |
| ShowColumnHeaders | Gets or sets a value indicating whether column headers should be displayed. |
| Source | Gets or sets the data source for the TreeDataGrid. |
AllowTriStateSorting Property
Gets or sets a value indicating whether the user can cycle through ascending, descending, and unsorted states when clicking the column header. The default is false.
public bool AllowTriStateSorting { get; set; }
Remarks
When true, clicking a column header will cycle the sort order through ascending, descending, and unsorted. When false, clicking a column header will cycle the sort order through ascending and descending only.
This behavior only applies if Avalonia.Controls.TreeDataGrid.CanUserSortColumns is true. Individual columns can override this behavior via their Avalonia.Controls.TreeDataGridColumn.AllowTriStateSorting property.
AutoDragDropRows Property
Gets or sets a value indicating whether rows can be automatically dragged and dropped within the TreeDataGrid.
public bool AutoDragDropRows { get; set; }
Remarks
When enabled, users can reorder rows by dragging them to a new position. Changes will be reflected in the underlying data source. The default value is false.
CanUserResizeColumns Property
Gets or sets a value indicating whether users can resize columns.
public bool CanUserResizeColumns { get; set; }
Remarks
When true, users can adjust column widths by dragging the column dividers. The default value is true.
CanUserSortColumns Property
Gets or sets a value indicating whether users can sort columns.
public bool CanUserSortColumns { get; set; }
Remarks
When true, clicking on a column header will sort the data by that column. The default value is true.
ColumnHeaderTheme Property
Gets or sets the theme applied to all column headers.
public Avalonia.Styling.ControlTheme ColumnHeaderTheme { get; set; }
Remarks
This theme controls the appearance of column header elements in the TreeDataGrid.
ColumnHeadersPresenter Property
Gets the presenter that displays the column headers.
public Avalonia.Controls.Primitives.TreeDataGridColumnHeadersPresenter ColumnHeadersPresenter { get; set; }
Remarks
The column headers presenter is defined in the control template and is responsible for rendering the column headers at the top of the TreeDataGrid.
Columns Property
Gets the columns defined on the Avalonia.Controls.TreeDataGrid.
public Avalonia.Controls.TreeDataGridColumns Columns { get; set; }
Remarks
This property can be populated in XAML or read from the Avalonia.Controls.TreeDataGrid.Source.
ElementFactory Property
Gets the element factory used to create UI elements for the TreeDataGrid.
public Avalonia.Controls.Primitives.TreeDataGridElementFactory ElementFactory { get; set; }
Remarks
The element factory is responsible for creating the visual elements that represent rows, cells, and other components of the TreeDataGrid.
FrozenColumnCount Property
Gets or sets the number of columns that are frozen on the left side.
public int FrozenColumnCount { get; set; }
Remarks
Frozen columns do not scroll horizontally and remain visible at all times.
The default value is 0, meaning no columns are frozen. A value greater than the total column count will freeze all columns.
ItemsSource Property
Gets or sets the items source for the TreeDataGrid.
public System.Collections.IEnumerable ItemsSource { get; set; }
Remarks
When set, a Avalonia.Controls.FlatTreeDataGridSource<T> is automatically created using the XAML-defined Avalonia.Controls.TreeDataGrid.Columns and assigned to Avalonia.Controls.TreeDataGrid.Source.
This property is mutually exclusive with setting Avalonia.Controls.TreeDataGrid.Source directly.
RowTheme Property
Gets or sets the theme applied to all rows.
public Avalonia.Styling.ControlTheme RowTheme { get; set; }
Remarks
This theme controls the appearance of row elements in the TreeDataGrid.
Rows Property
Gets the rows collection of the TreeDataGrid.
public Avalonia.Controls.TreeDataGridRows Rows { get; set; }
Remarks
This property is automatically populated from the Avalonia.Controls.TreeDataGrid.Source.
RowsPresenter Property
Gets the presenter that displays the rows.
public Avalonia.Controls.Primitives.TreeDataGridRowsPresenter RowsPresenter { get; set; }
Remarks
The rows presenter is defined in the control template and is responsible for rendering the rows of data in the TreeDataGrid.
Scroll Property
Gets the scroll viewer that enables scrolling through the TreeDataGrid content.
public Avalonia.Controls.Primitives.IScrollable Scroll { get; set; }
Remarks
The scroll viewer is defined in the control template and provides vertical and horizontal scrolling capabilities for the TreeDataGrid.
Selection Property
Gets the selection model for the TreeDataGrid.
public Avalonia.Controls.Selection.TreeDataGridSelectionModel Selection { get; set; }
Remarks
To access the strongly-typed selection model, cast to the appropriate type. In the case
where Avalonia.Controls.TreeDataGrid.Source is set, the selection model type is determined by the source.
When Avalonia.Controls.TreeDataGrid.ItemsSource is set, the selection object will be a
TreeDataGridRowSelectionModel<object> or a
TreeDataGridCellSelectionModel<object> depending on
Avalonia.Controls.TreeDataGrid.SelectionMode.
SelectionMode Property
Gets or sets the selection mode for the TreeDataGrid.
public Avalonia.Controls.TreeDataGridSelectionMode SelectionMode { get; set; }
Remarks
Note that this property cannot be set when Avalonia.Controls.TreeDataGrid.Source is set directly; the selection must be configured on the source itself in this case.
ShowColumnHeaders Property
Gets or sets a value indicating whether column headers should be displayed.
public bool ShowColumnHeaders { get; set; }
Remarks
The default value is true.
Source Property
Gets or sets the data source for the TreeDataGrid.
public Avalonia.Controls.TreeDataGridSource Source { get; set; }
Remarks
The source provides the data model, columns, and rows to be displayed in the TreeDataGrid.
Use Avalonia.Controls.FlatTreeDataGridSource<T> for flat data or Avalonia.Controls.HierarchicalTreeDataGridSource<T> for hierarchical data.
Fields
AllowTriStateSortingProperty Field
Defines the Avalonia.Controls.TreeDataGrid.AllowTriStateSorting property.
public Avalonia.StyledProperty<bool> AllowTriStateSortingProperty
AutoDragDropRowsProperty Field
Defines the Avalonia.Controls.TreeDataGrid.AutoDragDropRows property.
public Avalonia.StyledProperty<bool> AutoDragDropRowsProperty
CanUserResizeColumnsProperty Field
Defines the Avalonia.Controls.TreeDataGrid.CanUserResizeColumns property.
public Avalonia.StyledProperty<bool> CanUserResizeColumnsProperty
CanUserSortColumnsProperty Field
Defines the Avalonia.Controls.TreeDataGrid.CanUserSortColumns property.
public Avalonia.StyledProperty<bool> CanUserSortColumnsProperty
ColumnHeaderThemeProperty Field
Defines the Avalonia.Controls.TreeDataGrid.ColumnHeaderTheme property.
public Avalonia.StyledProperty<Avalonia.Styling.ControlTheme> ColumnHeaderThemeProperty
ColumnsProperty Field
Defines the Avalonia.Controls.TreeDataGrid.Columns property.
public Avalonia.DirectProperty<Avalonia.Controls.TreeDataGrid, Avalonia.Controls.TreeDataGridColumns> ColumnsProperty
ElementFactoryProperty Field
Defines the Avalonia.Controls.TreeDataGrid.ElementFactory property.
public Avalonia.DirectProperty<Avalonia.Controls.TreeDataGrid, Avalonia.Controls.Primitives.TreeDataGridElementFactory> ElementFactoryProperty
FrozenColumnCountProperty Field
Defines the Avalonia.Controls.TreeDataGrid.FrozenColumnCount property.
public Avalonia.StyledProperty<int> FrozenColumnCountProperty
ItemsSourceProperty Field
Defines the Avalonia.Controls.TreeDataGrid.ItemsSource property.
public Avalonia.StyledProperty<System.Collections.IEnumerable> ItemsSourceProperty
RowDragOverEvent Field
Defines the Avalonia.Controls.TreeDataGrid.RowDragOver event.
public Avalonia.Interactivity.RoutedEvent<Avalonia.Controls.TreeDataGridRowDragEventArgs> RowDragOverEvent
RowDragStartedEvent Field
Defines the Avalonia.Controls.TreeDataGrid.RowDragStarted event.
public Avalonia.Interactivity.RoutedEvent<Avalonia.Controls.TreeDataGridRowDragStartedEventArgs> RowDragStartedEvent
RowDropEvent Field
Defines the Avalonia.Controls.TreeDataGrid.RowDrop event.
public Avalonia.Interactivity.RoutedEvent<Avalonia.Controls.TreeDataGridRowDragEventArgs> RowDropEvent
RowThemeProperty Field
Defines the Avalonia.Controls.TreeDataGrid.RowTheme property.
public Avalonia.StyledProperty<Avalonia.Styling.ControlTheme> RowThemeProperty
RowsProperty Field
Defines the Avalonia.Controls.TreeDataGrid.Rows property.
public Avalonia.DirectProperty<Avalonia.Controls.TreeDataGrid, Avalonia.Controls.TreeDataGridRows> RowsProperty
ScrollProperty Field
Defines the Avalonia.Controls.TreeDataGrid.Scroll property.
public Avalonia.DirectProperty<Avalonia.Controls.TreeDataGrid, Avalonia.Controls.Primitives.IScrollable> ScrollProperty
SelectionModeProperty Field
Defines the Avalonia.Controls.TreeDataGrid.SelectionMode property.
public Avalonia.StyledProperty<Avalonia.Controls.TreeDataGridSelectionMode> SelectionModeProperty
ShowColumnHeadersProperty Field
Defines the Avalonia.Controls.TreeDataGrid.ShowColumnHeaders property.
public Avalonia.StyledProperty<bool> ShowColumnHeadersProperty
SourceProperty Field
Defines the Avalonia.Controls.TreeDataGrid.Source property.
public Avalonia.DirectProperty<Avalonia.Controls.TreeDataGrid, Avalonia.Controls.TreeDataGridSource> SourceProperty
Events
| Name | Description |
|---|---|
| CellClearing | Occurs when a cell is about to be cleared from the visual tree. |
| CellPrepared | Occurs when a cell has been prepared for display. |
| CellValueChanged | Occurs when a cell's value has changed. |
| RowClearing | Occurs when a row is about to be cleared from the visual tree. |
| RowDragOver | Occurs when a drag operation is performed over a row. |
| RowDragStarted | Occurs when the user starts to drag a row. |
| RowDrop | Occurs when a drop operation is performed on a row. |
| RowPrepared | Occurs when a row has been prepared for display. |
| SelectionChanged | Occurs when the selection in the TreeDataGrid changes. |
| SelectionChanging | Occurs before the selection in the TreeDataGrid changes. |
CellClearing Event
Occurs when a cell is about to be cleared from the visual tree.
public event EventHandler<Avalonia.Controls.TreeDataGridCellEventArgs> CellClearing
Remarks
A cell is cleared when it is no longer needed for display, such as when it is scrolled out of view or when the data is removed from the data source. This event can be used to perform any necessary cleanup or state management before a cell is removed from the visual tree.
CellPrepared Event
Occurs when a cell has been prepared for display.
public event EventHandler<Avalonia.Controls.TreeDataGridCellEventArgs> CellPrepared
Remarks
A cell is prepared when it is created and bound to data for display. This event can be used to customize the appearance or behavior of the cell before it is rendered.
CellValueChanged Event
Occurs when a cell's value has changed.
public event EventHandler<Avalonia.Controls.TreeDataGridCellEventArgs> CellValueChanged
Remarks
This event is raised after the cell's value has been updated, either through user interaction or programmatically. It can be used to respond to changes in cell values, such as updating related data or triggering validation.
RowClearing Event
Occurs when a row is about to be cleared from the visual tree.
public event EventHandler<Avalonia.Controls.TreeDataGridRowEventArgs> RowClearing
Remarks
A row is cleared when it is no longer needed for display, such as when it is scrolled out of view or when the data is removed from the data source. This event can be used to perform any necessary cleanup or state management before a row is removed from the visual tree.
RowDragOver Event
Occurs when a drag operation is performed over a row.
public event EventHandler<Avalonia.Controls.TreeDataGridRowDragEventArgs> RowDragOver
RowDragStarted Event
Occurs when the user starts to drag a row.
public event EventHandler<Avalonia.Controls.TreeDataGridRowDragStartedEventArgs> RowDragStarted
RowDrop Event
Occurs when a drop operation is performed on a row.
public event EventHandler<Avalonia.Controls.TreeDataGridRowDragEventArgs> RowDrop
RowPrepared Event
Occurs when a row has been prepared for display.
public event EventHandler<Avalonia.Controls.TreeDataGridRowEventArgs> RowPrepared
Remarks
A row is prepared when it is created and bound to data for display. This event can be used to customize the appearance or behavior of the row before it is rendered.
SelectionChanged Event
Occurs when the selection in the TreeDataGrid changes.
public event EventHandler<Avalonia.Controls.Selection.TreeDataGridSelectionChangedEventArgs> SelectionChanged
Remarks
This event is forwarded from the underlying selection model's Avalonia.Controls.Selection.TreeDataGridSelectionModel.SelectionChanged event, providing unified event args with row and cell index information.
SelectionChanging Event
Occurs before the selection in the TreeDataGrid changes.
public event System.ComponentModel.CancelEventHandler SelectionChanging
Remarks
This event can be cancelled to prevent the selection from changing.