Skip to content

Commit 1663ac7

Browse files
committed
Hide NavigationViewItem Chevron if Children is empty
Attach a CollectionChanged to ItemsRepeater ItemsViewSource whenever the MenuItems or MenuItemsSource changes. Add check to MenuItemsSource count when checking for children. Fixes microsoft#2743
1 parent 1c67178 commit 1663ac7

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

dev/NavigationView/NavigationViewItem.cpp

+8-1
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,16 @@ void NavigationViewItem::UpdateRepeaterItemsSource()
144144
}
145145
return MenuItems().as<winrt::IInspectable>();
146146
}();
147+
m_itemsSourceViewChanged.revoke();
147148
repeater.ItemsSource(itemsSource);
149+
m_itemsSourceViewChanged = repeater.ItemsSourceView().CollectionChanged(winrt::auto_revoke, { this, &NavigationViewItem::OnItemsSourceViewChanged });
148150
}
149151
}
152+
153+
void NavigationViewItem::OnItemsSourceViewChanged(const winrt::IInspectable& sender, const winrt::NotifyCollectionChangedEventArgs& args)
154+
{
155+
UpdateVisualStateForChevron();
156+
}
150157

151158
winrt::UIElement NavigationViewItem::GetSelectionIndicator() const
152159
{
@@ -468,7 +475,7 @@ void NavigationViewItem::UpdateVisualStateForChevron()
468475

469476
bool NavigationViewItem::HasChildren()
470477
{
471-
return MenuItems().Size() > 0 || MenuItemsSource() != nullptr || HasUnrealizedChildren();
478+
return MenuItems().Size() > 0 || (MenuItemsSource() != nullptr && m_repeater.get().ItemsSourceView().Count() > 0) || HasUnrealizedChildren();
472479
}
473480

474481
bool NavigationViewItem::ShouldShowIcon()

dev/NavigationView/NavigationViewItem.h

+2
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ class NavigationViewItem :
100100
bool HasChildren();
101101

102102
void UpdateRepeaterItemsSource();
103+
void OnItemsSourceViewChanged(const winrt::IInspectable& sender, const winrt::NotifyCollectionChangedEventArgs& args);
103104
void ReparentRepeater();
104105
void OnFlyoutClosing(const winrt::IInspectable& sender, const winrt::FlyoutBaseClosingEventArgs& args);
105106
void UpdateItemIndentation();
@@ -124,6 +125,7 @@ class NavigationViewItem :
124125

125126
winrt::ItemsRepeater::ElementPrepared_revoker m_repeaterElementPreparedRevoker{};
126127
winrt::ItemsRepeater::ElementClearing_revoker m_repeaterElementClearingRevoker{};
128+
winrt::ItemsSourceView::CollectionChanged_revoker m_itemsSourceViewChanged{};
127129

128130
winrt::FlyoutBase::Closing_revoker m_flyoutClosingRevoker{};
129131
winrt::Control::IsEnabledChanged_revoker m_isEnabledChangedRevoker{};

0 commit comments

Comments
 (0)