Skip to content

Commit

Permalink
NavigationView: Fix crash when launched in Top mode with hierarchical…
Browse files Browse the repository at this point in the history
  • Loading branch information
Kinnara committed Sep 11, 2020
1 parent 6fb9ff6 commit cbc9be8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
4 changes: 3 additions & 1 deletion ModernWpf.Controls/NavigationView/NavigationViewItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,9 @@ void UpdateVisualStateForChevron()

internal bool HasChildren()
{
return MenuItems.Count > 0 || (MenuItemsSource != null && m_repeater.ItemsSourceView.Count > 0) || HasUnrealizedChildren;
return MenuItems.Count > 0
|| (MenuItemsSource != null && m_repeater != null && m_repeater.ItemsSourceView.Count > 0)
|| HasUnrealizedChildren;
}

bool ShouldShowIcon()
Expand Down
24 changes: 21 additions & 3 deletions test/ModernWpfTestApp/ApiTests/NavigationViewTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,7 @@ public void VerifyMenuItemAndContainerMappingMenuItemsSource()
var navView = new NavigationView();
MUXControlsTestApp.App.TestContentRoot = navView;
navView.MenuItemsSource = new ObservableCollection<String> { "Item 1", "Item 2" }; ;
navView.MenuItemsSource = new ObservableCollection<string> { "Item 1", "Item 2" }; ;
navView.Width = 1008; // forces the control into Expanded mode so that the menu renders
MUXControlsTestApp.App.TestContentRoot.UpdateLayout(true);
Expand All @@ -855,11 +855,11 @@ public void VerifyMenuItemAndContainerMappingMenuItemsSource()
var menuItem = "Item 2";
// Get container for item
var itemContainer = navView.ContainerFromMenuItem(menuItem) as NavigationViewItem;
bool correctContainerReturned = itemContainer != null && (itemContainer.Content as String) == menuItem;
bool correctContainerReturned = itemContainer != null && (itemContainer.Content as string) == menuItem;
Verify.IsTrue(correctContainerReturned, "Correct container should be returned for passed in menu item.");
// Get item for container
var returnedItem = navView.MenuItemFromContainer(itemContainer) as String;
var returnedItem = navView.MenuItemFromContainer(itemContainer) as string;
bool correctItemReturned = returnedItem != null && returnedItem == menuItem;
Verify.IsTrue(correctItemReturned, "Correct item should be returned for passed in container.");
Expand Down Expand Up @@ -1080,5 +1080,23 @@ public void VerifyClearingItemsCollectionDoesNotCrashWhenItemSelectedOnTopNav()
itemsSource.Clear();
});
}

[TestMethod]
public void VerifyHierarchicalNavigationTopModeMenuItemsSourceDoesNotCrash()
{
RunOnUIThread.Execute(() =>
{
var navView = new NavigationView();
Content = navView;
navView.PaneDisplayMode = NavigationViewPaneDisplayMode.Top;
var childItem = new NavigationViewItem() { Content = "Item 1.1" };
var parentItem = new NavigationViewItem() { Content = "Item 1", MenuItemsSource = new ObservableCollection<NavigationViewItem>() { childItem } };
navView.MenuItemsSource = new ObservableCollection<NavigationViewItem>() { parentItem };
Content.UpdateLayout();
});
}
}
}

0 comments on commit cbc9be8

Please sign in to comment.