@@ -278,8 +278,9 @@ void NavigationView::SelectandMoveOverflowItem(winrt::IInspectable const& select
278
278
}
279
279
280
280
// We only need to close the flyout if the selected item is a leaf node
281
- void NavigationView::CloseFlyoutIfRequired ()
281
+ void NavigationView::CloseFlyoutIfRequired (const winrt::NavigationViewItem& selectedItem )
282
282
{
283
+ auto const selectedIndex = m_selectionModel.SelectedIndex ();
283
284
bool isInModeWithFlyout = [this ]()
284
285
{
285
286
if (auto splitView = m_rootSplitView.get ())
@@ -292,31 +293,18 @@ void NavigationView::CloseFlyoutIfRequired()
292
293
return false ;
293
294
}();
294
295
295
- if (isInModeWithFlyout)
296
+ if (isInModeWithFlyout && selectedIndex && ! DoesNavigationViewItemHaveChildren (selectedItem) )
296
297
{
297
- if (auto const selectedIndex = m_selectionModel.SelectedIndex ())
298
+ // Item selected is a leaf node, find top level parent and close flyout
299
+ if (auto const rootItem = GetContainerForIndex (selectedIndex.GetAt (0 )))
298
300
{
299
- if (auto const selectedItem = GetContainerForIndexPath (selectedIndex ))
301
+ if (auto const nvi = rootItem. try_as <winrt::NavigationViewItem>( ))
300
302
{
301
- if (auto const selectedNVI = selectedItem.try_as <winrt::NavigationViewItem>())
303
+ auto const nviImpl = winrt::get_self<NavigationViewItem>(nvi);
304
+ if (nviImpl->ShouldRepeaterShowInFlyout ())
302
305
{
303
- if (!DoesNavigationViewItemHaveChildren (selectedNVI))
304
- {
305
- // Item selected is a leaf node, find top level parent and close flyout
306
- if (auto const rootItem = GetContainerForIndex (selectedIndex.GetAt (0 )))
307
- {
308
- if (auto const nvi = rootItem.try_as <winrt::NavigationViewItem>())
309
- {
310
- auto const nviImpl = winrt::get_self<NavigationViewItem>(nvi);
311
- if (nviImpl->ShouldRepeaterShowInFlyout ())
312
- {
313
- nviImpl->ShowChildren (false );
314
- }
315
- }
316
- }
317
- }
306
+ nvi.IsExpanded (false );
318
307
}
319
-
320
308
}
321
309
}
322
310
}
@@ -773,7 +761,7 @@ void NavigationView::OnNavigationViewItemInvoked(const winrt::NavigationViewItem
773
761
774
762
if (updateSelection)
775
763
{
776
- CloseFlyoutIfRequired ();
764
+ CloseFlyoutIfRequired (nvi );
777
765
}
778
766
}
779
767
@@ -1231,7 +1219,7 @@ void NavigationView::OpenPane()
1231
1219
// Call this when you want an uncancellable close
1232
1220
void NavigationView::ClosePane ()
1233
1221
{
1234
- CollapseAllMenuItemsUnderRepeater (m_leftNavRepeater.get ());
1222
+ CollapseMenuItemsInRepeater (m_leftNavRepeater.get ());
1235
1223
auto scopeGuard = gsl::finally ([this ]()
1236
1224
{
1237
1225
m_isOpenPaneForInteraction = false ;
@@ -3371,7 +3359,7 @@ void NavigationView::OnPropertyChanged(const winrt::DependencyPropertyChangedEve
3371
3359
// When PaneDisplayMode is changed, reset the force flag to make the Pane can be opened automatically again.
3372
3360
m_wasForceClosed = false ;
3373
3361
3374
- CollapseAllMenuItems (auto_unbox (args.OldValue ()));
3362
+ CollapseTopLevelMenuItems (auto_unbox (args.OldValue ()));
3375
3363
UpdatePaneToggleButtonVisibility ();
3376
3364
UpdatePaneDisplayMode (auto_unbox (args.OldValue ()), auto_unbox (args.NewValue ()));
3377
3365
UpdatePaneTitleFrameworkElementParents ();
@@ -4678,7 +4666,7 @@ void NavigationView::ShowHideChildrenItemsRepeater(const winrt::NavigationViewIt
4678
4666
{
4679
4667
auto nviImpl = winrt::get_self<NavigationViewItem>(nvi);
4680
4668
4681
- nviImpl->ShowChildren (nvi. IsExpanded () );
4669
+ nviImpl->ShowHideChildren ( );
4682
4670
4683
4671
if (nviImpl->ShouldRepeaterShowInFlyout ())
4684
4672
{
@@ -4820,29 +4808,28 @@ winrt::IInspectable NavigationView::GetChildrenForItemInIndexPath(const winrt::U
4820
4808
return nullptr ;
4821
4809
}
4822
4810
4823
- void NavigationView::CollapseAllMenuItems (winrt::NavigationViewPaneDisplayMode oldDisplayMode)
4811
+ void NavigationView::CollapseTopLevelMenuItems (winrt::NavigationViewPaneDisplayMode oldDisplayMode)
4824
4812
{
4825
4813
// We want to make sure only top level items are visible when switching pane modes
4826
4814
if (oldDisplayMode == winrt::NavigationViewPaneDisplayMode::Top)
4827
4815
{
4828
- CollapseAllMenuItemsUnderRepeater (m_topNavRepeater.get ());
4829
- CollapseAllMenuItemsUnderRepeater (m_topNavRepeaterOverflowView.get ());
4816
+ CollapseMenuItemsInRepeater (m_topNavRepeater.get ());
4817
+ CollapseMenuItemsInRepeater (m_topNavRepeaterOverflowView.get ());
4830
4818
}
4831
4819
else
4832
4820
{
4833
- CollapseAllMenuItemsUnderRepeater (m_leftNavRepeater.get ());
4821
+ CollapseMenuItemsInRepeater (m_leftNavRepeater.get ());
4834
4822
}
4835
4823
}
4836
4824
4837
- void NavigationView::CollapseAllMenuItemsUnderRepeater (const winrt::ItemsRepeater& ir)
4825
+ void NavigationView::CollapseMenuItemsInRepeater (const winrt::ItemsRepeater& ir)
4838
4826
{
4839
4827
for (int index = 0 ; index < GetContainerCountInRepeater (ir); index ++)
4840
4828
{
4841
4829
if (auto const element = ir.TryGetElement (index ))
4842
4830
{
4843
4831
if (auto const nvi = element.try_as <winrt::NavigationViewItem>())
4844
4832
{
4845
- CollapseAllMenuItemsUnderRepeater (winrt::get_self<NavigationViewItem>(nvi)->GetRepeater ());
4846
4833
ChangeIsExpandedNavigationViewItem (nvi, false /* isExpanded*/ );
4847
4834
}
4848
4835
}
0 commit comments