Skip to content

Sync TableView SelectedItems with the always-selected row 0 (#13230) - #13235

Merged
niksedk merged 1 commit into
mainfrom
fix/13230-tableview-selecteditems-desync
Aug 5, 2026
Merged

Sync TableView SelectedItems with the always-selected row 0 (#13230)#13235
niksedk merged 1 commit into
mainfrom
fix/13230-tableview-selecteditems-desync

Conversation

@niksedk

@niksedk niksedk commented Aug 5, 2026

Copy link
Copy Markdown
Member

Fixes #13230.

The bug

After opening a subtitle, the first row looks selected and its text shows - then a few seconds later the text box, Show and Duration go blank while row 1 stays highlighted. A shift-selection that visually starts at row 1 also leaves row 1 out, so italic/copy/delete skip the first line.

Root cause

MakeTableView creates every grid with SelectionMode.AlwaysSelected. In Avalonia 12.1.1 that mode selects row 0 as soon as ItemsSource is assigned to a populated collection, but the pick only reaches the internal selection model - the row is drawn highlighted and SelectedItem/SelectedIndex point at it, while the SelectedItems collection stays empty and no SelectionChanged is raised. Verified headlessly:

after ItemsSource=items:  SelectedItem=line 1  SelectedIndex=0  Selection.Count=1
                          SelectedItems.Count=0        <-- desync
events: <none>

It does not heal on a layout pass, and neither Selection.Clear()+Select(0) nor an ItemsSource/SelectedIndex bounce repairs it - only moving the selection to a different row does, which is why clicking a row makes everything work again. Without AlwaysSelected the same sequence stays in sync, so the mode is the trigger.

Subtitle Edit reads every selection through SubtitleGridSelectedItems -> SubtitleGrid.SelectedItems, so the whole application saw nothing selected:

  • Blank edit box a few seconds in - the two-way SelectedItem binding does push row 0 into SelectedSubtitle, so the text shows at first. On the open-last-file-on-start path the restore chain ends in SetRecentFileProperties -> UpdateVideoOffsetStatus -> SubtitleGridSelectionChanged, which reads the empty SelectedItems, takes the "nothing selected" branch and sets SelectedSubtitle = null. That call runs after the awaited video open, which is the several-second delay - and it is why the repro needs a close-and-reopen rather than a plain File > Open.
  • Row 1 omitted from a selection - reproduced exactly. Applying the grid's own shift-select range over rows 1-5 yields Selection.Count=5 but SelectedItems=[line 2, line 3, line 4, line 5].

The #13190 fix does not cover this: its guard is !ReferenceEquals(SelectedSubtitle, itemToScroll), and the two-way binding has already set SelectedSubtitle, so it never fires - and if it did it would read the same empty SelectedItems.

The fix

Repair SelectedItems from the selection model on every ItemsSource change, in MakeTableView so every grid in the application is covered rather than the main subtitle grid alone. The repair is a no-op unless SelectedItems is empty while the selection model is not.

Tests

Four regression tests in tests/UI/Logic/TableViewSelectionSyncTests.cs, built on a grid configured like the main subtitle grid (multi-select, always-selected, two-way SelectedItem/SelectedIndex bindings) and filled the way SetSubtitles fills it. Three of them fail without the fix; the fourth is the empty-grid guard case. Full UI suite: 1417 passing.

🤖 Generated with Claude Code

SelectionMode.AlwaysSelected picks row 0 the moment ItemsSource is assigned to a
populated collection, but that pick only reaches the internal selection model:
the row is drawn highlighted and SelectedItem/SelectedIndex point at it, while
the SelectedItems collection stays empty and no SelectionChanged is raised.
Neither a layout pass nor a Selection.Clear()/Select(0) repairs it - only moving
the selection to another row does.

Subtitle Edit reads every selection through SelectedItems, so the grid showed the
first row highlighted while the application saw nothing selected (#13230):

- the edit box, Show and Duration went blank a few seconds after start-up, when
  the open-last-file restore reached SetRecentFileProperties ->
  UpdateVideoOffsetStatus -> SubtitleGridSelectionChanged, which read the empty
  SelectedItems and cleared SelectedSubtitle
- a shift-selection starting at row 1 left row 1 out of the selection, so italic,
  copy, delete and friends silently skipped the first line

Repair the collection from the selection model on every ItemsSource change, in
MakeTableView so every grid in the application is covered rather than the main
subtitle grid alone.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@niksedk
niksedk merged commit fd80344 into main Aug 5, 2026
1 check passed
@niksedk
niksedk deleted the fix/13230-tableview-selecteditems-desync branch August 5, 2026 10:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5,2 release ~ 5.2 beta 4 -- When you open a subtitle the 1st entry doesn't seem to be selected.

1 participant