Navigation page doesn't highlight the selected page

Eduardo Gomez 3,631 Reputation points
2025-04-27T17:47:17.47+00:00

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

User's image

as you can see, I have the calendar page by default but is not highlighted in the NavigationView

Windows App SDK
Windows App SDK
A set of Microsoft open-source libraries, frameworks, components, and tools to be used in apps to access Windows platform functionality on many versions of Windows. Previously known as Project Reunion.
877 questions
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 89,061 Reputation points
    2025-04-28T07:29:52.34+00:00

    You must select the item, like :

    rootContainer.SelectedItem = rootContainer.MenuItems[0]
    

    in Loaded event


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.