How To Create Login Page In Wpf C# Mvvm

How To Articles

Creating a login page in WPF using C# and MVVM is a fundamental task in building secure and user-friendly applications. In this article, I will guide you through the step-by-step process of creating a login page that follows the MVVM (Model-View-ViewModel) architectural pattern.

Before We Begin

Before diving into the implementation, let’s take a moment to understand the significance of the MVVM pattern in building WPF applications. MVVM separates the logic and presentation layers, making the code more maintainable and testable. It provides a robust foundation for building scalable and modular applications.

Step 1: Create a New WPF Project

To start, open Visual Studio and create a new WPF project. Choose the appropriate project template and give it a meaningful name. Once the project is created, you will see the default MainWindow.xaml file.

Step 2: Design the Login Page

Next, let’s design the login page interface. Open the MainWindow.xaml file and add the necessary XAML code. You can use various WPF controls like TextBlocks, TextBoxes, and Buttons to create an intuitive and visually appealing login form. Don’t forget to add labels and placeholders for the username and password fields.

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    
    <TextBlock Text="Login Page" FontSize="20" FontWeight="Bold" HorizontalAlignment="Center"/>
    
    <TextBlock Text="Username:" Grid.Row="1" Margin="10"/>
    <TextBox Grid.Row="1" Margin="10,0,10,10" Width="200"/>
    
    <TextBlock Text="Password:" Grid.Row="2" Margin="10"/>
    <PasswordBox Grid.Row="2" Margin="10,0,10,10" Width="200"/>
    
    <Button Content="Login" Grid.Row="3" Margin="10" Width="100"/>
</Grid>

Step 3: Implement the ViewModel

In the MVVM pattern, the ViewModel acts as an intermediary between the View and the Model. It contains the business logic and exposes properties and commands that the View can bind to. Create a new class called LoginViewModel and implement the necessary properties and commands.

public class LoginViewModel : INotifyPropertyChanged
{
    private string username;
    public string Username
    {
        get { return username; }
        set
        {
            if (username != value)
            {
                username = value;
                OnPropertyChanged(nameof(Username));
            }
        }
    }
    
    private ICommand loginCommand;
    public ICommand LoginCommand
    {
        get
        {
            if (loginCommand == null)
            {
                loginCommand = new RelayCommand(Login);
            }
            return loginCommand;
        }
    }
    
    // Implement INotifyPropertyChanged interface
    // and define OnPropertyChanged method
}

Step 4: Bind the View to the ViewModel

In the MainWindow.xaml file, set the Window.DataContext property to an instance of the LoginViewModel. This will establish the data context for the Login page and allow us to bind the controls to the ViewModel properties.

<Window x:Class="YourNamespace.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:YourNamespace"
        Title="Login Page" Height="300" Width="400">
    <Window.DataContext>
        <local:LoginViewModel />
    </Window.DataContext>
    
    
    
</Window>

Step 5: Implement the Login Logic

Finally, let’s add the login logic to the LoginViewModel. When the user clicks the Login button, the LoginCommand will be executed and validate the entered username and password. You can add your desired login validation logic inside the Login method.

private void Login()
{
    if (Username == "admin" && Password == "password")
    {
        // Navigate to the main application page
    }
    else
    {
        // Display an error message
    }
}

Conclusion

In this article, we learned how to create a login page in WPF using C# and MVVM. By following the MVVM pattern, we separated the UI logic from the business logic, making our code more maintainable and testable. Remember to customize the login page design and add your own logic for authentication and navigation.

For a live example of a login page built with WPF and MVVM, you can check out the demo login page on our website.