Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ItemsSource and TreeViewNode syncing #1202

Merged
merged 3 commits into from
Aug 24, 2019
Merged

Conversation

kaiguo
Copy link
Contributor

@kaiguo kaiguo commented Aug 23, 2019

Fixes #1182

In expand/collapse scenario, TreeViewNode.Children already contains the right content for ItemsSource, which means we don't need to clear and re-populate ItemsSource into children collection again. Re-populating for every expand can potentially create an infinite loop because updating TreeViewNode.Children collection triggers ListView layout updates, which can trigger ItemsSource update in some cases (when there are multiple levels of children and TreeViewItems got recycled), if we clean and re-populate children collection again it becomes an infinite loop.

Added a check to make sure we only update TreeViewNode children collection when the contents are actually different from ItemsSource.

@kaiguo kaiguo requested a review from a team as a code owner August 23, 2019 21:36
@kaiguo kaiguo self-assigned this Aug 23, 2019
}

// Compare the actual content in collections when counts are equal
if (itemsSourceCount > 0)
Copy link
Contributor

Choose a reason for hiding this comment

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

This 'if' statement is not needed.

{
for (UINT32 i = 0; i < itemsSourceCount; i++)
{
if (Children().GetAt(i).Content() != m_itemsDataSource.GetAt(i))
Copy link
Contributor

Choose a reason for hiding this comment

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

How about accessing and caching Children() exactly once at the top of the method?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated :)

{
for (UINT32 i = 0; i < itemsSourceCount; i++)
{
if (Children().GetAt(i).Content() != m_itemsDataSource.GetAt(i))
Copy link
Contributor

Choose a reason for hiding this comment

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

Can Children().GetAt(i) be null?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There's a if (childrenCount != itemsSourceCount) check at the top, so we shouldn't get here if GetAt(i) is null.

@@ -2688,6 +2688,61 @@ public void TreeViewRootNodeBindingTest()
}
}

[TestMethod]
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like the InteractionTests.TreeViewTests.TreeViewSwappingNodesTest_ContentMode test regressed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looks like the InteractionTests.TreeViewTests.TreeViewSwappingNodesTest_ContentMode test regressed.

Just saw your reviews...
The test is setting ItemsSource to null and I was accessing itemsSource.Count without any check, which caused crash, should be fixed now.

@kaiguo kaiguo merged commit 165fc3c into master Aug 24, 2019
@kaiguo kaiguo deleted the user/kaiguo/treeview-fix branch August 24, 2019 04:38
@jevansaks jevansaks added the release note PR that we want to call out in the next release summary label Aug 26, 2019
@msft-github-bot
Copy link
Collaborator

🎉Microsoft.UI.Xaml v2.2.190830001 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
Labels
area-TreeView release note PR that we want to call out in the next release summary
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TreeView with ItemTemplate "removes" Items unexpectedly
4 participants