microsoft / microsoft/microsoft-ui-xaml
TreeView does not respect initial SelectedItem when initialized
- Dominant language
- C++
- Stars
- 8.4k
- Forks
- 942
- Avg merge
- 2d 7h
- Merged PRs (30d)
- 105
Description
### Describe the bug
I created a reproducible project [here](https://github.com/AndrewKeepCoding/TreeViewSelectedItemBugSample) but is something like the following:
```cs
public partial class ShellViewModel : ObservableObject
{
[ObservableProperty]
private string[] _items =
[
"Item 1",
"Item 2",
"Item 3",
];
[ObservableProperty]
private string? _selectedItem;
public ShellViewModel()
{
SelectedItem = Items[1];
}
[RelayCommand]
private void SelectFirstItem() => SelectedItem = Items.First();
[RelayCommand]
private void SelectLastItem() => SelectedItem = Items.Last();
}
```
```xaml
```
As you can see, the `TreeView` control is not respecting the bound `SelectedItem` right after it's initialized. It should have "Item 2" selected. And you can also see that the binding is not broken.
https://github.com/user-attachments/assets/352fae30-d9ff-4fb1-8b02-609922eaeccd
### Steps to reproduce the bug
Use the repo above.
### Expected behavior
`TreeView` respects `SelectedItem` when loaded.
### Screenshots
_No response_
### NuGet package version
WinUI 3 - Windows App SDK 1.6.1: 1.6.240923002
### Windows version
Windows Insider Build (xxxxx)
### Additional context
_No response_
Contributor guide
Research direction
Start with the linked TreeViewSelectedItemBugSample and reproduce the initialization behavior using the XAML SelectedItem binding shown in the issue. Compare TreeView initialization with the ListView, GridView, and NavigationView examples, then verify that Item 2 is selected when the control loads while later two-way binding still works.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- desktop, frontend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100