Skip to content

Commit

Permalink
Fix TreeView missing children issue (#2058)
Browse files Browse the repository at this point in the history
* Add fix for missing treeview children

* Add visual test for issue scenario

* Leaner test logic

* Add interactive test

* Revert "Add interactive test"

This reverts commit c11c644.

* Add interactive tests

* Move ItemsSource PropertyChanged logic into own function

* Fixed failing tests
  • Loading branch information
Felix-Dev authored Mar 24, 2020
1 parent c0fb694 commit f083cde
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 10 deletions.
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"/>
</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())
{
if (treeViewNode->ItemsSource() == nullptr)
{
treeViewItem->SetItemsSource(targetNode, itemsSource);
}
}

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

0 comments on commit f083cde

Please sign in to comment.