Skip to content

Report deselected items and batch SelectAll in TreeView - #22172

Merged
grokys merged 5 commits into
AvaloniaUI:mainfrom
Evan260:fix-treeview-selection-removed
Oct 7, 2026
Merged

grokys merged 5 commits into
AvaloniaUI:mainfrom
Evan260:fix-treeview-selection-removed

Conversation

@Evan260

@Evan260 Evan260 commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

What does the pull request do?

Addresses two of the three things reported in #22116: deselected items aren't reported, and SelectAll raises one event per item.

The third point in that issue, ISelectionModel support for TreeView, is a public API addition rather than a bug, so I've left it alone and haven't used Fixes.

What is the current behavior?

Selecting one item and then another logs:

Added: 1, removed: 0 (selected: 1)
Added: 1, removed: 0 (selected: 1)

The first item is deselected but never reported. Ctrl+A on four items raises four separate events.

What is the updated/expected behavior with this PR?

Added: 1, removed: 1 (selected: 1)

Ctrl+A raises one event containing all the items. This matches what ListBox already does.

How was the solution implemented (if it's not obvious)?

SelectSingleItem clears SelectedItems and then adds to it. AvaloniaList.Clear raises a Reset, and a reset carries no OldItems, so there was nothing to report the deselection from. The control now keeps the previous selection so the reset case can work out what went away.

Doing only that would turn one event into two, so the clear and the add are now batched and raised as a single event. SelectAll uses the same batching.

I used a separate flag for the batching rather than reusing _syncingSelectedItems, since that one also gates some SelectedItem bookkeeping that shouldn't change. Both call sites use try/finally so a failure part way through can't leave the control swallowing events.

Checklist

Two tests, both of which fail on main.

Breaking changes

Handlers that assumed RemovedItems was always empty, or that counted one event per item from SelectAll, will see different values. The selection itself is unchanged.

Related issues

Part of #22116

Selecting a different item clears and repopulates SelectedItems. The clear
raises a Reset, which carries no OldItems, so the deselection was dropped and
only the addition was reported. Track the previous selection so the reset can
work out what was deselected, and raise the clear and the add as one event.

SelectAll added items one at a time, raising an event per item. Batch it the
same way.
@avaloniaui-bot

Copy link
Copy Markdown

You can test this PR using the following package version. 12.2.999-cibuild0069596-alpha. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

@MrJul MrJul added bug backport-candidate-12.1.x Consider this PR for backporting to 12.1 branch area-controls labels Sep 8, 2026
Comment thread src/Avalonia.Controls/TreeView.cs Resolved

@grokys grokys left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See previous comment that the LLM posted without requesting changes, sigh.

Keep a single snapshot of the last reported selection and diff against it,
instead of tracking the snapshot and the pending changes separately. A reset
that keeps some items no longer reports them as added.

Co-authored-by: Steven Kirk <grokys@users.noreply.github.com>
@Evan260
Evan260 requested a review from grokys September 11, 2026 15:35
@avaloniaui-bot

Copy link
Copy Markdown

You can test this PR using the following package version. 12.2.999-cibuild0069845-alpha. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

@github-actions

github-actions Bot commented Oct 6, 2026 •

Copy link
Copy Markdown

NuGet packages

The packages for this pull request have been published (workflow run):

@grokys
grokys force-pushed the fix-treeview-selection-removed branch from f29cad2 to 205399e Compare October 7, 2026 07:39
grokys added 2 commits October 7, 2026 09:40
No other fields have comments, pretty much in the whole codebase. This isn't an exception.

@grokys grokys left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, much simpler! I removed a comment on a field (almost no other fields in Avalonia merit a comment, and I don't think this one was really an exception)

@grokys
grokys enabled auto-merge October 7, 2026 07:42
@grokys
grokys added this pull request to the merge queue Oct 7, 2026
Merged via the queue into AvaloniaUI:main with commit 8c34f2b Oct 7, 2026
11 checks passed
@Evan260
Evan260 deleted the fix-treeview-selection-removed branch October 7, 2026 19:10
MrJul pushed a commit that referenced this pull request Oct 9, 2026
* Report deselected items and batch SelectAll in TreeView

Selecting a different item clears and repopulates SelectedItems. The clear
raises a Reset, which carries no OldItems, so the deselection was dropped and
only the addition was reported. Track the previous selection so the reset can
work out what was deselected, and raise the clear and the add as one event.

SelectAll added items one at a time, raising an event per item. Batch it the
same way.

* Simplify TreeView selection tracking

Keep a single snapshot of the last reported selection and diff against it,
instead of tracking the snapshot and the pending changes separately. A reset
that keeps some items no longer reports them as added.

Co-authored-by: Steven Kirk <grokys@users.noreply.github.com>

* Simplify comments

* Remove comment.

No other fields have comments, pretty much in the whole codebase. This isn't an exception.

---------

Co-authored-by: Steven Kirk <grokys@users.noreply.github.com>
@MrJul MrJul added backported-12.1.x and removed backport-candidate-12.1.x Consider this PR for backporting to 12.1 branch labels Oct 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants