Skip to content

This repository contains a sample explaining how to toggle NavigationDrawer via MVVM command in .NET MAUI.

Notifications You must be signed in to change notification settings

SyncfusionExamples/How-to-Toggle-NavigationDrawer-via-MVVM-Command-in-.NET-MAUI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

This article explains how to toggle the .NET MAUI NavigationDrawer using MVVM commands in an application. Using commands instead of event handlers enhances modularity, testability, and maintains a clean separation between UI and logic.

ViewModel

Define a ViewModel with a property to track the drawer's state and a command to toggle it:

public class MainPageViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler? PropertyChanged;
    
    public void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    private bool _isDrawerOpen;
    public bool IsDrawerOpen
    {
        get => _isDrawerOpen;
        set
        {
            _isDrawerOpen = value;
            OnPropertyChanged(nameof(IsDrawerOpen));
        }
    }

    public ICommand ToggleDrawerCommand { get; }

    public MainPageViewModel()
    {
        ToggleDrawerCommand = new Command(ToggleDrawer);
    }

    private void ToggleDrawer()
    {
        IsDrawerOpen = !IsDrawerOpen;
    }
}

Setting the BindingContext

Ensure that the ViewModel is set as the BindingContext of the page:

<ContentPage.BindingContext>
    <local:MainPageViewModel/>
</ContentPage.BindingContext>

XAML

Bind the IsOpen property of NavigationDrawer to IsDrawerOpen and the toggle command to an ImageButton in the XAML file:

<navigationdrawer:SfNavigationDrawer x:Name="navigationDrawer" IsOpen="{Binding IsDrawerOpen, Mode=TwoWay}">
    <navigationdrawer:SfNavigationDrawer.ContentView>
        <Grid x:Name="mainContentView" BackgroundColor="White" RowDefinitions="Auto,*">
            <HorizontalStackLayout BackgroundColor="#6750A4" Spacing="10" Padding="5,0,0,0">
                <ImageButton x:Name="hamburgerButton"
                             Source="hamburgericon.png"
                             Command="{Binding ToggleDrawerCommand}"/>
            </HorizontalStackLayout>
        </Grid>
    </navigationdrawer:SfNavigationDrawer.ContentView>
</navigationdrawer:SfNavigationDrawer>

By following these steps, you can toggle the NavigationDrawer using an MVVM command instead of event handlers, making your application more modular and testable.

Output

MauiDrawer_Toggle.gif

Requirements to run the demo

To run the demo, refer to System Requirements for .NET MAUI

Troubleshooting:

Path too long exception

If you are facing path too long exception when building this example project, close Visual Studio and rename the repository to short and build the project.

About

This repository contains a sample explaining how to toggle NavigationDrawer via MVVM command in .NET MAUI.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages