You must select the item, like :
rootContainer.SelectedItem = rootContainer.MenuItems[0]
in Loaded event
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I am using a NavigatinonView
<winex:WindowEx
x:Class="HorizonHub.View.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Interactivity="using:Microsoft.Xaml.Interactivity"
xmlns:local="using:HorizonHub.View"
xmlns:winex="using:WinUIEx"
Title="MainWindow">
<NavigationView
x:Name="rootContainer"
Margin="0,50,0,0"
IsBackButtonVisible="Collapsed"
IsPaneToggleButtonVisible="False"
IsSettingsVisible="False"
PaneDisplayMode="Left">
<Interactivity:Interaction.Behaviors>
<Interactivity:EventTriggerBehavior EventName="SelectionChanged">
<Interactivity:InvokeCommandAction Command="{x:Bind MainWindowModel.NavigateCommand}" />
</Interactivity:EventTriggerBehavior>
</Interactivity:Interaction.Behaviors>
<!-- Avatar & Dropdown at the TOP -->
<NavigationView.PaneCustomContent>
<StackPanel
Margin="10,0,0,5"
Orientation="Horizontal">
<Button
Background="Transparent"
BorderBrush="Transparent"
FocusVisualPrimaryBrush="Transparent"
FocusVisualSecondaryBrush="Transparent">
<Button.Style>
<Style TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<ContentPresenter />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Button.Style>
<Button.Flyout>
<Flyout
x:Name="OptionsMenu"
Placement="Bottom">
<StackPanel Orientation="Horizontal">
<TextBlock
Margin="0,0,0,4"
VerticalAlignment="Center"
Text="Compact Mode" />
<ToggleSwitch
Margin="10,0,0,0"
IsOn="{Binding IsCompactModeEnabled, Mode=TwoWay}" />
</StackPanel>
</Flyout>
</Button.Flyout>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="28" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Image
Width="28"
Height="28"
Source="/AppIcon.ico" />
<TextBlock
Grid.Column="1"
Margin="10,0,0,0"
VerticalAlignment="Center"
Text="Options" />
</Grid>
</Button>
</StackPanel>
</NavigationView.PaneCustomContent>
<NavigationView.MenuItems>
<NavigationViewItem
Content="Calendar"
Icon="Calendar"
Tag="CalendarPage" />
<NavigationViewItem
Content="Account"
Icon="People"
Tag="AccountPage" />
<NavigationViewItem
Content="About"
Icon="Help"
Tag="AboutPage" />
</NavigationView.MenuItems>
<ContentControl
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Content="{Binding CurrentPage}" />
</NavigationView>
</winex:WindowEx>
and in the vm, I am setting the current to the CalendarPage
public partial class MainWindowViewModel : ObservableObject {
private const string IsCompactModeEnabledKey = "IsCompactModeEnabled";
private readonly ApplicationDataContainer _localSettings = ApplicationData.Current.LocalSettings;
private readonly CalendarPageViewModel _calendarPageViewModel;
public Action<bool>? isCompactMode;
[ObservableProperty]
bool isCompactModeEnabled;
[ObservableProperty]
object? currentPage;
public MainWindowViewModel(CalendarPageViewModel calendarPageViewModel) {
_calendarPageViewModel = calendarPageViewModel;
if(_localSettings.Values.TryGetValue(IsCompactModeEnabledKey, out var storedValue)
&& storedValue is bool savedBool) {
IsCompactModeEnabled = savedBool;
}
else {
IsCompactModeEnabled = false; // Default value
}
CurrentPage = new CalendarPage(_calendarPageViewModel);
}
[RelayCommand]
void Navigate(NavigationViewSelectionChangedEventArgs args) {
if(args.SelectedItem is NavigationViewItem selectedItem && selectedItem.Tag is string pageTag) {
CurrentPage = pageTag switch {
"CalendarPage" => new CalendarPage(_calendarPageViewModel),
"AccountPage" => new AccountPage(),
"AboutPage" => new AboutPage(),
_ => new CalendarPage(_calendarPageViewModel) // Default fallback
};
}
}
partial void OnIsCompactModeEnabledChanged(bool value) {
isCompactMode?.Invoke(value);
_localSettings.Values["IsCompactModeEnabled"] = value;
}
}
}
but the navigation View doesn't select it by default
as you can see, I have the calendar page by default but is not highlighted in the NavigationView
You must select the item, like :
rootContainer.SelectedItem = rootContainer.MenuItems[0]
in Loaded event