With the help of the following codes, I can find the NavigationViewItem I want
var item = navigationView.MenuItems
.OfType<NavigationViewItem>()
.FirstOrDefault(menuItem => (string)menuItem.Content == (string)args.InvokedItem);
but If a NavigationViewItem itself has other menuitems, this code is no longer valid. How can I find the NavigationViewItem I want among all the menuitems?
CodePudding user response:
Assuming that NavigationViewItem
also has a collection of MenuItems
, you could use some self referencing method like this:
(bool, NavigationViewItem?) SearchItems(List<NavigationViewItems> itemsToSearch, string argsInvokedItem) {
foreach (var item in itemsToSearch)
{
if((string)item.Content == argsInvokedItem) {
return (true, item);
}
if(item.MenuItems.Count > 0) {
if(SearchItems(item.MenuItems, argsInvokedItem).Item1)
return (true, item);
}
}
return (false, null);
}
And then call it like this:
var result = SearchItems(navigationView.MenuItems, (string)args.InvokedItem)