NavigationView pane isnt recognizing boolean

Eduardo Gomez 3,631 Reputation points
2025-05-03T05:40:46.94+00:00

I am using storage to save and retrieve the status of the pane in the navigationView

 <NavigationView
     x:Name="NavView"
     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>



So, when I press the button and enable coompactMode, I hide the pane and save it

    public partial class MainWindowViewModel : ObservableObject {

        private const string IsCompactModeEnabledKey = "IsCompactModeEnabled";
        private readonly ApplicationDataContainer _localSettings = ApplicationData.Current.LocalSettings;

        public Action<bool>? isCompactMode;

        [ObservableProperty]
        Type? currentPageType;

        [ObservableProperty]
        bool isCompactModeEnabled;

        public MainWindowViewModel() {

            if(_localSettings.Values.TryGetValue(IsCompactModeEnabledKey, out var storedValue)
                && storedValue is bool savedBool) {

                IsCompactModeEnabled = savedBool;
            }
            else {
                IsCompactModeEnabled = false; // Default value
            }

            CurrentPageType = typeof(CalendarPage);

        }

        [RelayCommand]
        void Navigate(NavigationViewSelectionChangedEventArgs args) {
            if(args.SelectedItem is NavigationViewItem selectedItem && selectedItem.Tag is string pageTag) {

                var assembly = Assembly.GetExecutingAssembly(); // Get current assembly

                CurrentPageType = assembly.GetType($"HorizonHub.View.{pageTag}");
            }
        }

        partial void OnIsCompactModeEnabledChanged(bool value) {

            isCompactMode?.Invoke(value);

            _localSettings.Values["IsCompactModeEnabled"] = value;

        }
    }
}

But If I hide the pane and start the app again, the pane is open

Untitled video - Made with Clipchamp (4)

code

https://github.com/eduardoagr/Horizon-Hub

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

1 answer

Sort by: Most helpful
  1. Michael Hawker 0 Reputation points Microsoft Employee
    2025-05-08T08:17:15.04+00:00

    Hi Eduardo,

    It looks like you're bleeding between your View and ViewModel layers. I recommend checking out this example and presentation Sergio and I did for .NET Conf 2024: https://github.com/michael-hawker/MVVMNetConfApp

    The whole separate function you're providing to do interop between layers seems fishy. This is what x:Bind/Binding is for. Generally, yes, you should bind your boolean in your VM to the IsPaneOpen, but you may be encountering this bug? https://github.com/microsoft/microsoft-ui-xaml/issues/7609 - if so, you should be sure to comment on it. I believe in one of the older threads there was mention of using the Loaded event as an instigator to changing the property.


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.