Get All MenuItems in NavigationView UWP

I would like to iterate through all the elements inside NavigationView.MenuItems in my program to find similar ones, but the MenuItem that was created inside another MenuItem cannot be found:

    <muxc:NavigationView x:Name="NavView">
            <muxc:NavigationView.MenuItems>
                <muxc:NavigationViewItem x:Uid="navBarButtonHome" Tag="Home" IsSelected="True"/>
                <muxc:NavigationViewItem x:Uid="navBarButtonAdd" Tag="Create">
                    <muxc:NavigationViewItem.MenuItems>
                        <muxc:NavigationViewItem x:Uid="navBarButtonSubNote" Tag="NewNote"/>
                        <muxc:NavigationViewItem x:Uid="navBarButtonSubCategory" Tag="NewCategory"/>
                        <muxc:NavigationViewItem x:Uid="navBarButtonSubStore" Tag="NewStore"/>
                        <muxc:NavigationViewItem x:Uid="navBarButtonSubDuty" Tag="NewDuty"/>
                        <muxc:NavigationViewItem x:Uid="navBarButtonSubCurrency" Tag="NewCurrency"/>
                    </muxc:NavigationViewItem.MenuItems>
                </muxc:NavigationViewItem>
                <muxc:NavigationViewItem x:Uid="navBarButtonTags" Tag="Tags"/>
                <muxc:NavigationViewItem x:Uid="navBarButtonChart" Tag="Chart"/>

            </muxc:NavigationView.MenuItems>
        </muxc:NavigationView>

NavigationView.MenuItems return all items except those which contains in MenuItem with tag "Create" ("NewNote", "NewCategory" etc).

How I can get all menuItems with those ("NewNote", "NewCategory" etc)?

>Solution :

NavigationView.MenuItems returns a IList<object>, you should cast each element as a NavigationViewItem and check for additional MenuItems.

var items = _navigationViewControl.MenuItems;
foreach (var item in items)
{
    var viewItem = item as WinUI.NavigationViewItem;
    if (viewItem.MenuItems.Count > 0)
    {
        ...
    }
}

Leave a Reply