microsoft-ui-xaml icon indicating copy to clipboard operation
microsoft-ui-xaml copied to clipboard

TreeViewItem.IsExpanded in WindowsAppSDK 1.6/1.7/1.8 behaves inconsistently when repopulating the TreeView

Open usakgb opened this issue 11 months ago • 6 comments

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

  1. Clone and run the project.
  2. Click Repopulate button a few times.

Expected behavior

All items to be expanded at all times.

Screenshots

Image

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

usakgb avatar Jan 14 '25 20:01 usakgb

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:

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

github-actions[bot] avatar Jan 17 '25 19:01 github-actions[bot]

Yeah, I have the same issue..

bogdan-patraucean avatar Mar 06 '25 17:03 bogdan-patraucean

Same issue here in 1.7:

<TreeView>
    <TreeView.ItemTemplate>
        <DataTemplate>
            <TreeViewItem
                IsExpanded="True"
                ...

When adding new items into the tree, the elements are collapsed.

weitzhandler avatar Apr 23 '25 05:04 weitzhandler

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.

michael-hawker avatar May 09 '25 07:05 michael-hawker

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.

lansdon-qualta avatar Oct 27 '25 18:10 lansdon-qualta

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).

aicd0 avatar Dec 10 '25 16:12 aicd0