Search code examples
c#xamluwpnavigationview

How to change the properties of NavigaitonViewItems


MainPage.xaml

 <NavigationView x:Name="NPPngv" x:FieldModifier="public">
       <NavigationView.MenuItems>
          <NavigationViewItem Content="Customer"/>
          <NavigationViewItem Content="Deliverer"/>
          <NavigationViewItem Content="Admin"/>
       </NavigationView.MenuItems>
       <Frame x:Name="contentFrame"/>
  </NavigationView>

Is there a way to change a property for each of the NavigationViewItems For example I want set all the items to IsEnabled=false but I need it to be repeatable and with any number of items.

Is it possible to create an array of the items and then iterate though it?


Solution

  • The MenuItems property returns an IList<object> so you could do this in the constructor of your Page:

    public MainPage()
    {
        this.InitializeComponent();
    
        foreach (var item in NPPngv.MenuItems.OfType<NavigationViewItem>())
        {
            item.IsEnabled = false;
        }
    }
    

    Don't forget to add using System.Linq; at the top of your source code file.