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 TreeView missing children issue #2058

28 changes: 28 additions & 0 deletions dev/TreeView/InteractionTests/TreeViewTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2815,6 +2815,34 @@ public void ItemsSourceResyncTest()
}
}

[TestMethod]
[TestProperty("TestSuite", "B")]
public void ItemsSourceSwitchForthAndBackTest()
{
// TreeView databinding only works on RS5+
if (IsLowerThanRS5())
{
return;
}

using (var setup = new TestSetupHelper("TreeView Tests"))
{
SetContentMode(true);

ClickButton("SwapItemsSource");
Wait.ForIdle();
ClickButton("GetItemCount");
Verify.AreEqual("2", ReadResult());

ClickButton("SwapItemsSource");
Wait.ForIdle();
ClickButton("ExpandRootNode");
Wait.ForIdle();
ClickButton("GetItemCount");
Verify.AreEqual("4", ReadResult());
}
}

private void ClickButton(string buttonName)
{
var button = new Button(FindElement.ByName(buttonName));
Expand Down
1 change: 1 addition & 0 deletions dev/TreeView/TestUI/TreeViewPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
<Button x:Name="ResetItemsSource" AutomationProperties.Name="ResetItemsSource" Content="ResetItemsSource" HorizontalAlignment="Stretch" Margin="1" Click="ResetItemsSource_Click"/>
<Button x:Name="ResetItemsSourceAsync" AutomationProperties.Name="ResetItemsSourceAsync" Content="ResetItemsSourceAsync" HorizontalAlignment="Stretch" Margin="1" Click="ResetItemsSourceAsync_Click"/>
<Button x:Name="ExpandRootNode" AutomationProperties.Name="ExpandRootNode" Content="ExpandRootNode" HorizontalAlignment="Stretch" Margin="1" Click="ExpandRootNode_Click"/>
<Button x:Name="SwapItemsSource" AutomationProperties.Name="SwapItemsSource" Content="SwapItemsSource" HorizontalAlignment="Stretch" Margin="1" Click="SwapItemsSource_Click"/>
Felix-Dev marked this conversation as resolved.
Show resolved Hide resolved
</StackPanel>
</ScrollViewer>
<ScrollViewer Grid.Column="1">
Expand Down
12 changes: 12 additions & 0 deletions dev/TreeView/TestUI/TreeViewPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -998,5 +998,17 @@ private void ExpandRootNode_Click(object sender, RoutedEventArgs e)
{
TestTreeViewItemsSource[0].IsExpanded = !TestTreeViewItemsSource[0].IsExpanded;
}

private void SwapItemsSource_Click(object sender, RoutedEventArgs e)
{
if (ContentModeTestTreeView.ItemsSource == TestTreeView2ItemsSource)
{
this.ContentModeTestTreeView.ItemsSource = TestTreeViewItemsSource;
}
else
{
this.ContentModeTestTreeView.ItemsSource = TestTreeView2ItemsSource;
}
}
}
}
8 changes: 8 additions & 0 deletions dev/TreeView/TreeViewList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ void TreeViewList::OnContainerContentChanging(const winrt::IInspectable& /*sende
if (!args.InRecycleQueue())
{
auto targetItem = args.ItemContainer().as<winrt::TreeViewItem>();

// "Re-evaluate" the item source binding to trigger a TreeViewItem.OnPropertyChanged(ItemsSourceProperty) call.
if (auto itemsSource = targetItem.ItemsSource())
Felix-Dev marked this conversation as resolved.
Show resolved Hide resolved
{
targetItem.ItemsSource(nullptr);
Felix-Dev marked this conversation as resolved.
Show resolved Hide resolved
targetItem.ItemsSource(itemsSource);
}

auto targetNode = NodeFromContainer(targetItem);
auto treeViewItem = winrt::get_self<TreeViewItem>(targetItem);
treeViewItem->UpdateIndentation(targetNode.Depth());
Expand Down