Another way to do this, with less code, is by getting the target items and changing the properties directly.
You can get the target items using the VisualTreeHelper
but it's easier using the CommunityToolkit.WinUI.UI NuGet package.
First, name your NavigationView
control, e.g. RightNavigationViewControl.
<NavigationView
x:Name="RightNavigationViewControl"
SelectionChanged="NavigationView_SelectionChanged">
<NavigationView.MenuItems>
<NavigationViewItem
Content="Home"
Icon="Home"
Tag="RightNavigationViewExample.HomePage">
<NavigationViewItem.MenuItems>
<NavigationViewItem
Content="Page A"
Tag="RightNavigationViewExample.PageA" />
</NavigationViewItem.MenuItems>
</NavigationViewItem>
</NavigationView.MenuItems>
<Frame x:Name="ContentFrame" />
</NavigationView>
Then get the target items using the FindDescendant
extension method from the CommunityToolkit.WinUI.UI and change the properties like this:
using CommunityToolkit.WinUI.UI;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using System;
namespace RightNavigationViewExample;
public sealed partial class ShellPage : Page
{
public ShellPage()
{
this.InitializeComponent();
this.RightNavigationViewControl.Loaded += RightNavigationViewControl_Loaded;
}
private void RightNavigationViewControl_Loaded(object sender, RoutedEventArgs e)
{
if (this.RightNavigationViewControl.FindDescendant<Grid>(x => x.Name is "PaneToggleButtonGrid") is Grid paneToggleButtonGrid &&
this.RightNavigationViewControl.FindDescendant<SplitView>(x => x.Name is "RootSplitView") is SplitView rootSplitView &&
this.RightNavigationViewControl.FindDescendant<Grid>(x => x.Name is "PaneContentGrid") is Grid paneContentGrid)
{
paneToggleButtonGrid.HorizontalAlignment = HorizontalAlignment.Right;
rootSplitView.PanePlacement = SplitViewPanePlacement.Right;
paneContentGrid.HorizontalAlignment = HorizontalAlignment.Right;
}
}
}