Skip to content

Conversation

kaiguo
Copy link
Contributor

@kaiguo kaiguo commented Feb 2, 2020

Description

We made changes in previous PR #1917 to update expand state when ItemsSource changes, it doesn't work as expected when ItemsSource is set in an async way. PrepareContainerForItemOverride is a better place to do this, this should keep the property value and actual expand state always in sync.

Motivation and Context

Fix #1790.

How Has This Been Tested?

Added more interaction tests.

@msft-github-bot msft-github-bot added the needs-triage Issue needs to be triaged by the area owners label Feb 2, 2020
@kaiguo kaiguo added area-TreeView and removed needs-triage Issue needs to be triaged by the area owners labels Feb 2, 2020
@kaiguo
Copy link
Contributor Author

kaiguo commented Feb 3, 2020

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@kaiguo
Copy link
Contributor Author

kaiguo commented Feb 3, 2020

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@@ -320,6 +320,14 @@ void TreeViewList::PrepareContainerForItemOverride(winrt::DependencyObject const
{
bool hasChildren = itemContainer.HasUnrealizedChildren() || itemNode->HasChildren();
itemContainer.GlyphOpacity(hasChildren ? 1.0 : 0.0);
if (itemContainer.IsExpanded() != itemNode->IsExpanded()) {
const DispatcherHelper dispatcher{ *this };
Copy link
Contributor

@ranjeshj ranjeshj Feb 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious why you need to run this though the dispatcher async.

Also, should'nt it be the other way ? we are creating the container based on the data, so I would think we need to set the container's value to what the node has.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious why you need to run this though the dispatcher async.

It crashes if we don't use a dispatcher, I think it's still the same reason, changing expand state will change ListView's ItemsSource as we are adding/removing items from ListView, and this can't be done during layout.

Also, should'nt it be the other way ? we are creating the container based on the data, so I would think we need to set the container's value to what the node has.

Yeah this is confusing, in content mode, TreeViewItem (which is the container) holds the truth since we are using data binding, and in node mode it's the opposite, TreeViewNode holds the truth (at line 334 we are updating container based on TreeViewNode's state).

@@ -331,7 +331,6 @@ void TreeViewItem::OnPropertyChanged(const winrt::DependencyPropertyChangedEvent
// The children have changed, validate and update GlyphOpacity
bool hasChildren = HasUnrealizedChildren() || treeViewNode->HasChildren();
GlyphOpacity(hasChildren ? 1.0 : 0.0);
UpdateNodeIsExpandedAsync(*treeViewNode, IsExpanded());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you still need to do it in the case where the container is already prepared and then the source gets set on the node after that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think PrepareContainerForItem will get called every time we change ItemsSource? There were some odd behaviors when keeping them both in place. I guess it caused some race conditions because the operation is async...

Copy link
Contributor

@StephenLPeters StephenLPeters left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

@kaiguo kaiguo merged commit 7e1baad into master Feb 5, 2020
@kaiguo kaiguo deleted the user/kaiguo/treeview-expand branch February 5, 2020 18:52
@msft-github-bot
Copy link
Collaborator

🎉Microsoft.UI.Xaml v2.4.0-prerelease.200322001 has been released which incorporates this pull request.:tada:

Handy links:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TreeViewItem Template IsExpanded ignored when reused
5 participants