TreeSelectionModelBase<T> Class
Definition
Base class for selection models in TreeDataGrid.
public class TreeSelectionModelBase<T>
Remarks
TreeSelectionModelBase provides the core functionality for hierarchical selection, supporting both single and multiple selection modes. It tracks selection using Avalonia.Controls.IndexPath instances that represent positions in the hierarchical structure.
This class implements the Avalonia.Controls.Selection.ITreeSelectionModel interface and provides events for tracking selection changes, property changes, and changes to the underlying data structure.
Derived classes must implement Avalonia.Controls.Selection.TreeSelectionModelBase<T>.GetChildren(`0) to define how the hierarchical structure is navigated.
Methods
| Name | Description |
|---|---|
| BatchUpdate | Creates a batch update operation that will defer selection change notifications until disposed. |
| BeginBatchUpdate | Begins a batch update of the selection. |
| Clear | Clears the selection. |
| Deselect | Deselects an item by its index in the source data. |
| EndBatchUpdate | Ends a batch update started by Avalonia.Controls.Selection.ITreeSelectionModel.BeginBatchUpdate. |
| IsSelected | Queries whether an item is currently selected. |
| Select | Selects an item by its index in the source data. |
BatchUpdate Method
Creates a batch update operation that will defer selection change notifications until disposed.
public Avalonia.Controls.Selection.TreeSelectionModelBase<T>.BatchUpdateOperation<T><T> BatchUpdate()
Returns
Avalonia.Controls.Selection.TreeSelectionModelBase<T>.BatchUpdateOperation<T><T>
A disposable object that, when disposed, will commit the selection changes and raise appropriate events.
Remarks
Use this method with a using statement to group multiple selection operations together and raise only a single set of change events when the batch is complete. This is equivalent to calling Avalonia.Controls.Selection.TreeSelectionModelBase<T>.BeginBatchUpdate and Avalonia.Controls.Selection.TreeSelectionModelBase<T>.EndBatchUpdate.
BeginBatchUpdate Method
Begins a batch update of the selection.
public void BeginBatchUpdate()
Remarks
During a batch update no changes to the selection will be reflected in the model's properties and no events will be raised until Avalonia.Controls.Selection.ITreeSelectionModel.EndBatchUpdate is called.
Avalonia.Controls.Selection.ITreeSelectionModel.BeginBatchUpdate may be called multiple times, even when a batch update is already in progress; each call must have a corresponding call to Avalonia.Controls.Selection.ITreeSelectionModel.EndBatchUpdate in order to finish the operation.
Clear Method
Clears the selection.
public void Clear()
Deselect Method
Deselects an item by its index in the source data.
public void Deselect(Avalonia.Controls.IndexPath index)
Parameters
index Avalonia.Controls.IndexPath
The index into the source data.
EndBatchUpdate Method
Ends a batch update started by Avalonia.Controls.Selection.ITreeSelectionModel.BeginBatchUpdate.
public void EndBatchUpdate()
IsSelected Method
Queries whether an item is currently selected.
public bool IsSelected(Avalonia.Controls.IndexPath index)
Parameters
index Avalonia.Controls.IndexPath
The index of the item in the source data.
Returns
bool
True if the item is selected; otherwise false.
Select Method
Selects an item by its index in the source data.
public void Select(Avalonia.Controls.IndexPath index)
Parameters
index Avalonia.Controls.IndexPath
The index path of the item to select.
Remarks
If Avalonia.Controls.Selection.ITreeSelectionModel.SingleSelect is true, this method will clear any existing selection before selecting the specified item.
If the item is already selected, this method does nothing.
Properties
| Name | Description |
|---|---|
| AnchorIndex | Gets or sets the anchor index. |
| Count | Gets the number of selected items. |
| RangeAnchorIndex | Gets or sets the range anchor index. |
| SelectedIndex | Gets or sets the selected index within the data. |
| SelectedIndexes | Gets the indexes of the selected items. |
| SelectedItem | Gets the currently selected item. |
| SelectedItems | Gets a collection containing all selected items. |
| SingleSelect | Gets or sets a value indicating whether the model supports single or multiple selection. |
AnchorIndex Property
Gets or sets the anchor index.
public Avalonia.Controls.IndexPath AnchorIndex { get; set; }
Remarks
The anchor index holds the index of the item from which non-ranged keyboard selection will start, i.e. in a vertical list it will hold the item from which selection should be moved by pressing the up or down arrow key. This is usually the last selected item.
Avalonia.Controls.Selection.ITreeSelectionModel.AnchorIndex is automatically set when selecting an item via Avalonia.Controls.Selection.ITreeSelectionModel.SelectedIndex or Avalonia.Controls.Selection.ITreeSelectionModel.Select(Avalonia.Controls.IndexPath).
Count Property
Gets the number of selected items.
public int Count { get; set; }
RangeAnchorIndex Property
Gets or sets the range anchor index.
public Avalonia.Controls.IndexPath RangeAnchorIndex { get; set; }
Remarks
The range anchor index holds the index of the item from which ranged selection will start, i.e. when shift-clicking an item it represents the start of the range to be selected whereas the shift-clicked item will be the end of the range.
Avalonia.Controls.Selection.ITreeSelectionModel.RangeAnchorIndex is set when selecting an item via Avalonia.Controls.Selection.ITreeSelectionModel.SelectedIndex but not via Avalonia.Controls.Selection.ITreeSelectionModel.Select(Avalonia.Controls.IndexPath).
SelectedIndex Property
Gets or sets the selected index within the data.
public Avalonia.Controls.IndexPath SelectedIndex { get; set; }
Remarks
Setting this property clears any existing selection and selects only the item at the specified index path. When multiple items are selected, this property represents the first selected item.
SelectedIndexes Property
Gets the indexes of the selected items.
public System.Collections.Generic.IReadOnlyList<Avalonia.Controls.IndexPath> SelectedIndexes { get; set; }
SelectedItem Property
Gets the currently selected item.
public T SelectedItem { get; set; }
Value
The selected item, or default(T) if no item is selected.
Remarks
When multiple items are selected, this property returns the first selected item.
SelectedItems Property
Gets a collection containing all selected items.
public System.Collections.Generic.IReadOnlyList<T> SelectedItems { get; set; }
Value
A read-only list of the selected items.
SingleSelect Property
Gets or sets a value indicating whether the model supports single or multiple selection.
public bool SingleSelect { get; set; }
Events
| Name | Description |
|---|---|
| IndexesChanged | Occurs when item indexes change due to insertions or removals in the data source. |
| PropertyChanged | Occurs when a property value changes. |
| SelectionChanged | Occurs when the selection changes. |
| SourceReset | Occurs when the data source is reset. |
IndexesChanged Event
Occurs when item indexes change due to insertions or removals in the data source.
public event EventHandler<Avalonia.Controls.Selection.TreeSelectionModelIndexesChangedEventArgs> IndexesChanged
PropertyChanged Event
Occurs when a property value changes.
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged
SelectionChanged Event
Occurs when the selection changes.
public event EventHandler<Avalonia.Controls.Selection.TreeSelectionModelSelectionChangedEventArgs<T><T>> SelectionChanged
Remarks
This event provides strongly-typed access to the selection changes, including the items and index paths that were selected and deselected.
SourceReset Event
Occurs when the data source is reset.
public event EventHandler<Avalonia.Controls.Selection.TreeSelectionModelSourceResetEventArgs> SourceReset
Remarks
This event is raised when a collection in the hierarchical data structure is reset, such as when a collection is cleared or replaced with a new collection.
Due to design limitations of collection reset events, the TreeSelectionModel cannot automatically preserve selection state when a collection is reset. The reset event doesn't provide information about which items were removed or how they map to new items.
Applications that need to maintain selection across collection resets must handle this event and manually restore the selection based on their knowledge of the data model. For example, you might store the IDs or unique identifiers of selected items before the reset, and then re-select them after the reset by finding the new indices of those items.
The Avalonia.Controls.Selection.TreeSelectionModelSourceResetEventArgs.ParentIndex property indicates which part of the hierarchical structure was reset, allowing you to determine whether to restore selection for the entire collection or just a subtree.