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;
}
}
}
}
23 changes: 13 additions & 10 deletions dev/TreeView/TreeViewItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,16 +322,7 @@ void TreeViewItem::OnPropertyChanged(const winrt::DependencyPropertyChangedEvent
}
else if (property == s_ItemsSourceProperty)
{
winrt::IInspectable value = args.NewValue();

auto treeViewNode = winrt::get_self<TreeViewNode>(node);
treeViewNode->ItemsSource(value);
if (IsInContentMode())
{
// The children have changed, validate and update GlyphOpacity
bool hasChildren = HasUnrealizedChildren() || treeViewNode->HasChildren();
GlyphOpacity(hasChildren ? 1.0 : 0.0);
}
SetItemsSource(node, args.NewValue());
}
else if (property == s_HasUnrealizedChildrenProperty)
{
Expand All @@ -341,6 +332,18 @@ void TreeViewItem::OnPropertyChanged(const winrt::DependencyPropertyChangedEvent
}
}

void TreeViewItem::SetItemsSource(winrt::TreeViewNode const& node, winrt::IInspectable const& value)
{
auto treeViewNode = winrt::get_self<TreeViewNode>(node);
treeViewNode->ItemsSource(value);
if (IsInContentMode())
{
// The children have changed, validate and update GlyphOpacity
bool hasChildren = HasUnrealizedChildren() || treeViewNode->HasChildren();
GlyphOpacity(hasChildren ? 1.0 : 0.0);
}
}

void TreeViewItem::OnExpandContentTimerTick(const winrt::IInspectable& /*sender*/, const winrt::IInspectable& /*e*/)
{
if (m_expandContentTimer)
Expand Down
2 changes: 2 additions & 0 deletions dev/TreeView/TreeViewItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class TreeViewItem :

void OnPropertyChanged(const winrt::DependencyPropertyChangedEventArgs& args);

void SetItemsSource(winrt::TreeViewNode const& node, winrt::IInspectable const& value);

public:
// IFrameworkElementOverrides
void OnApplyTemplate();
Expand Down
11 changes: 11 additions & 0 deletions dev/TreeView/TreeViewList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,18 @@ void TreeViewList::OnContainerContentChanging(const winrt::IInspectable& /*sende
{
auto targetItem = args.ItemContainer().as<winrt::TreeViewItem>();
auto targetNode = NodeFromContainer(targetItem);

auto treeViewItem = winrt::get_self<TreeViewItem>(targetItem);
auto treeViewNode = winrt::get_self<TreeViewNode>(targetNode);

if (auto itemsSource = targetItem.ItemsSource())
Felix-Dev marked this conversation as resolved.
Show resolved Hide resolved
{
if (treeViewNode->ItemsSource() == nullptr)
{
treeViewItem->SetItemsSource(targetNode, itemsSource);
}
}

treeViewItem->UpdateIndentation(targetNode.Depth());
treeViewItem->UpdateSelectionVisual(winrt::get_self<TreeViewNode>(targetNode)->SelectionState());
}
Expand Down