TreeViewItem.IsExpanded in WindowsAppSDK 1.6/1.7/1.8 behaves inconsistently when repopulating the TreeView
Describe the bug
When using TreeView's item template and setting TreeViewItem.IsExpanded to True in XAML, repopulating (clearing item and then adding new items) the tree a few times collapses all items. When binding TreeViewItem.IsExpanded to a read-only property set to True, repopulating the tree randomly collapses/expands the items. 1.5 doesn't have this issue.
Steps to reproduce the bug
A project that demonstrates the bug: https://github.com/usakgb/WinUITreeViewBug
- Clone and run the project.
- Click Repopulate button a few times.
Expected behavior
All items to be expanded at all times.
Screenshots
NuGet package version
Windows App SDK 1.6.3: 1.6.241114003
Packaging type
Packaged (MSIX)
Windows version
Windows 11 version 22H2 (22621, 2022 Update)
IDE
Visual Studio 2022
Additional context
No response
Hi I'm an AI powered bot that finds similar issues based off the issue title.
Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one. Thank you!
Open similar issues:
- TreeViewItem does not handle IsExpanded binding correctly (#10140), similarity score: 0.84
Closed similar issues:
- TreeViewItem doesn't always handle IsExpanded binding properly (#2112), similarity score: 0.82
TreeViewdoesn't preserve the value ofIsExpandedwhen containers in "content mode" are recycled and reused (#9549), similarity score: 0.81
Note: You can give me feedback by thumbs upping or thumbs downing this comment.
Yeah, I have the same issue..
Same issue here in 1.7:
<TreeView>
<TreeView.ItemTemplate>
<DataTemplate>
<TreeViewItem
IsExpanded="True"
...
When adding new items into the tree, the elements are collapsed.
It seems we have a lot of duplicates of this issue, I wonder which should be made champion. The original issue #2112 is the oldest, but was more about WinUI 2. And the other various ones have varying levels of details. Maybe #10140 should be the new main one?
In the meantime, I wonder if the issue has to do with parent/child loading order in OnApplyTemplate and this extra check for IsInContentMode done here:
https://github.com/microsoft/microsoft-ui-xaml/blob/268e06867114c04cc0f25201b57f64e326113ba0/src/controls/dev/TreeView/TreeViewItem.cpp#L279-L283
As the bound property probably retriggers a property changed event which then updates the state, vs. the fixed case where it's probably set by the time we get to OnApplyTemplate (if I remember my ordering correctly, it's been a bit since I've thought about one of these types of issues). IIRC the children get realized first, so then they may not have the parent reference yet? This seems like a good topic for a video... or at least something better to be called out more in the docs.
Though, I'm not sure of the purpose of IsInContentMode. For the TokenizingTextBox in the Windows Community Toolkit, we gave a reference to the parent control to the items in the PrepareContainerForItemOverride vs. walking up the logical/visual tree, but TreeView isn't an ItemsControl, so things work differently here.
Just some passing thoughts taking a quick look at the code.
This is a major bug that is blocking us from upgrading WindowsAppSDK past 1.5! PLEASE address this! As stated here it has been reported many times. TreeView is a major UI component for many applications.
I also encountered this issue and seems like there is no nice workaround (not even an ugly one except for delay a few milliseconds and set IsExpanded property again).