Selection Internals and Batch Update

This article explains TreeDataGrid selection contracts below the basic row/cell usage layer.

Selection Stack

The internal stack has three levels:

  1. Data model contracts:
  2. Interaction adapter between control input and selection state:
  3. Concrete models:

Batch Update Semantics

Use batching when applying many selection changes.

Two public patterns:

Example: Bulk Row Selection

using Avalonia.Controls;
using Avalonia.Controls.Selection;

if (source.RowSelection is TreeDataGridRowSelectionModel<Person> rows)
{
    using var update = rows.BatchUpdate();

    rows.Clear();
    rows.Select(new IndexPath(0));
    rows.Select(new IndexPath(2));
    rows.Select(new IndexPath(4));
}

Batching delays expensive notifications and keeps selection-change events coherent.

Index and Source Change Events

Key event args:

Important behavior:

  • SelectionChanged can be skipped by source Reset semantics in INotifyCollectionChanged providers.
  • Always pair SelectionChanged handling with SourceReset when you need durable consistency.

Node-Level Infrastructure

SelectionNodeBase<T> manages range state for each tree level and updates selection ranges on source changes.

You usually do not instantiate it directly, but it is the core reason hierarchical selection remains stable across add/remove/replace operations.

Column Selection Model

ITreeDataGridColumnSelectionModel and TreeDataGridColumnSelectionModel represent selected columns and are composed by cell-selection logic.

Troubleshooting

  • Duplicate or noisy selection events Cause: batch boundaries are missing during grouped updates.

  • Selection disappears after source reset Cause: consumer listens only to SelectionChanged and ignores SourceReset.

  • Keyboard/pointer behavior differs after custom selection assignment Cause: assigned selection does not implement ITreeDataGridSelectionInteraction.

API Coverage Checklist