简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM