Reputation: 181
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?
Upvotes: 0
Views: 139
Reputation: 169150
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.
Upvotes: 1
Reputation: 5868
If you want to create any number of items,you can bind MenuItemsSource with a model.And If you want to enable or disable NavigationViewItem, you can make IsEnabled property in the model then bind it.
.xaml
<Page.Resources>
<local:NavigationItemTemplateSelector x:Key="NavigationItemTemplateSelector">
<local:NavigationItemTemplateSelector.ItemTemplate>
<DataTemplate x:DataType="local:ViewModel" >
<NavigationViewItem Content="{x:Bind Name,Mode=OneWay}"
IsEnabled="{x:Bind IsEnabled,Mode=OneWay}" >
</NavigationViewItem>
</DataTemplate>
</local:NavigationItemTemplateSelector.ItemTemplate >
</local:NavigationItemTemplateSelector>
</Page.Resources>
<NavigationView x:Name="NPPngv" MenuItemsSource="{x:Bind NavigationList,Mode=OneWay}"
MenuItemTemplateSelector="{StaticResource NavigationItemTemplateSelector}">
<Frame x:Name="ContentFrame"/>
</NavigationView>
.cs:
[ContentProperty(Name = "ItemTemplate")]
public class NavigationItemTemplateSelector : DataTemplateSelector
{
public DataTemplate ItemTemplate { get; set; }
protected override DataTemplate SelectTemplateCore(object item)
{
return ItemTemplate;
}
}
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
NavigationList = new ObservableCollection<ViewModel>();
NavigationList.Add(new ViewModel { Name = "item1", IsEnabled = false });
NavigationList.Add(new ViewModel { Name = "item2", IsEnabled = false });
NavigationList.Add(new ViewModel { Name = "item3", IsEnabled = false });
NavigationList.Add(new ViewModel { Name = "item4", IsEnabled = false });
}
private ObservableCollection<ViewModel> NavigationList { get; set; }
}
Upvotes: 1