Repository navigation
Description
Current behavior 🐛
In a single-selection TreeView, select a row, scroll until that row has no container (virtualized), then clear the selection from code:
| Cleared via | Right after clearing | After scrolling the row back into view |
|---|---|---|
SelectedNode = null or SelectedNodes.Clear() |
SelectedNode is null, but SelectedItem still returns the row |
the row is selected again (SelectedNode and SelectedItem) |
SelectedItem = null |
SelectedNode and SelectedItem are null |
SelectedNode is the row again, SelectedItem stays null |
In all three cases the inner TreeViewList keeps the row selected (SelectedIndex unchanged): unselecting a node that has no container never updates the list's selection. When the row is realized again, Selector.PrepareContainerForItemOverride sets the container's IsSelected from that stale list selection, and TreeViewList.PrepareContainerForItemOverride (the workaround for #15214) then selects the node again. This happens with or without an IsSelected binding in the item template.
Expected behavior 🎯
Scrolling the row back into view does not select it again. Native WinUI (WinAppSDK 1.7) behaves like this:
| Cleared via | Right after clearing | After scrolling the row back into view |
|---|---|---|
SelectedNode = null or SelectedNodes.Clear() |
SelectedNode is null, SelectedItem still returns the row (same as Uno), no SelectionChanged |
SelectionChanged removes the row; SelectedNode and SelectedItem are null; a two-way bound IsSelected becomes false |
SelectedItem = null |
SelectedNode and SelectedItem are null, no SelectionChanged |
same as above |
So WinUI also leaves the list's selection stale while the row has no container, but completes the unselection when the row is realized, where Uno selects the row again.
How to reproduce it (as minimally and precisely as possible) 🔬
var items = Enumerable.Range(1, 100).Select(i => $"Node {i}").ToList();
var treeView = new TreeView { Height = 300, Width = 300, ItemsSource = items };
// add treeView to the window and wait for it to load
// 1. Click "Node 1" to select it.
// 2. Scroll to the end, so that "Node 1" has no container:
var scrollViewer = /* the ScrollViewer inside treeView */;
scrollViewer.ChangeView(null, scrollViewer.ScrollableHeight, null, disableAnimation: true);
// 3. Clear the selection:
treeView.SelectedNodes.Clear(); // or treeView.SelectedNode = null; or treeView.SelectedItem = null;
// → treeView.SelectedItem is still "Node 1" (SelectedNodes.Clear / SelectedNode = null)
// 4. Scroll back to the top:
scrollViewer.ChangeView(null, 0, null, disableAnimation: true);
// → "Node 1" is selected again: treeView.SelectedNode?.Content == "Node 1"Workaround 🛠️
None verified. With #25800, selecting another row instead of clearing works (the list's selection follows the new row); clearing while the selected row is realized should also work, since its container then updates the list.
Affected platforms 📱💻🖥️
All platforms 🌍 (reproduced on Desktop (Windows); the code involved is shared by all platforms)
Uno.Sdk version (and other relevant versions) 📦
Current master (Uno.WinUI 7.0.0-dev.2494 and later). Not specific to the fix in #25800: before it, the same code path assigned the already-selected item to the list again, which changed nothing.
IDE version 🧑💻
No response
Anything else we need to know? 💬
Found while working on #25796 / #25800. The WinUI results above come from a WinAppSDK 1.7 app with the same scenario, selecting the row by setting its container's IsSelected (as a click does). WinUI has no equivalent of Uno's no-container fallback in TreeViewViewModel.NotifyContainerOfSelectionChange, and no equivalent of the #15214 block that selects a node from its container's IsSelected while it is prepared.
When fixing this, note that replacing the selection (A → B) goes through "unselect A, then select B", so clearing the list's selection as soon as A is unselected would briefly report a null selection.
Activity