Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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)?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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)
    {
        ...
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading