Hi @Douglas Harris Welcome to Microsoft Q&A!
You can dynamically add Navigation View items using NavigationView.MenuItems.
Here is a minimal sample written according to your needs, you can refer to it.
The NavigationView used in the sample is a WinUI control, you need to install the latest version Microsoft.UI.Xaml in the NuGet Package Manager.
MainPage.xaml
// xmlns:muxc="using:Microsoft.UI.Xaml.Controls"
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="9*"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<muxc:NavigationView Grid.Column="0" x:Name="NavigationViewControl" ItemInvoked="NavigationViewControl_ItemInvoked_1">
<muxc:NavigationView.MenuItems>
<muxc:NavigationViewItem Icon="Home" Content="Home"/>
<muxc:NavigationViewItemSeparator/>
<muxc:NavigationViewItem Icon="Keyboard" Content="Main Item">
<muxc:NavigationViewItem.MenuItems>
<muxc:NavigationViewItem Content="Item"/>
<muxc:NavigationViewItem Content="Sub Item"/>
</muxc:NavigationViewItem.MenuItems>
</muxc:NavigationViewItem>
</muxc:NavigationView.MenuItems>
</muxc:NavigationView>
<StackPanel x:Name="SettingPanel" Background="HotPink" Grid.Column="1" Orientation="Vertical" Visibility="Collapsed">
<Button Click="Button_Click">AddSubItem</Button>
</StackPanel>
</Grid>
MainPage.xaml.cs
// using MUXC = Microsoft.UI.Xaml.Controls;
private void NavigationViewControl_ItemInvoked_1(Microsoft.UI.Xaml.Controls.NavigationView sender, Microsoft.UI.Xaml.Controls.NavigationViewItemInvokedEventArgs args)
{
if (args.IsSettingsInvoked)
{
if (SettingPanel.Visibility == Visibility.Collapsed)
{
SettingPanel.Visibility = Visibility.Visible;
}
else
{
SettingPanel.Visibility = Visibility.Collapsed;
}
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
MUXC.NavigationViewItem newSubItem = new MUXC.NavigationViewItem();
newSubItem.Content = "newSubItem";
foreach (var item in NavigationViewControl.MenuItems)
{
var menuItem = item as MUXC.NavigationViewItem;
var selectItem = NavigationViewControl.SelectedItem as MUXC.NavigationViewItem;
//find selectedItem
if (menuItem!=null &&
selectItem!=null &&
menuItem.Content.ToString() == selectItem.Content.ToString())
{
menuItem.MenuItems.Add(newSubItem);
}
}
}
Click the Setting button, AddSubItem Button will show on the right. Then select the MenuItem which you want to add sub-items and click AddSubItem Button.
Thank you,
Junjie
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.