Simple MVVM Pattern in WPF

Introduction

This article describes the basic use and functionality of the MVVM pattern in WPF.

The Model View ViewModel (MVVM) is an architectural pattern used in software engineering that originated from Microsoft, which specializes in the Presentation Model design pattern. It is based on the Model-view-controller pattern (MVC), and is targeted at modern UI development platforms (WPF and Silverlight) in which there is a UX developer who has different requirements than a more "traditional" developer. MVVM is a way of creating client applications that leverage core features of the WPF platform, allows for simple unit testing of application functionality, and helps developers and designers work together with less technical difficulties.

What is a VIEW?

A View is defined in XAML and should not have any logic in the code behind it. It binds to the view model by only using data binding.

What is MODEL?

A Model is responsible for exposing data in a way that is easily consumable by WPF. It must implement INotifyPropertyChanged and/or INotifyCollectionChanged as appropriate.

What is ViewModel?

A ViewModel is a model for a view in the application or, we can say, an abstraction of the view. It exposes data relevant to the view and exposes the behaviors for the views, usually with Commands.

Getting Started

  • Creating a WPF Project. Open Visual Studio 2010.
  • Go to File => New => Project
  • Select Window in installed templates
  • Select WPF Application
  • Enter the Name and choose the location.
  • Click OK

Now create three folders in the root application. The name should be Model, View, ViewModel, and now add a new class in the Model folder. My class name is User, and add this namespace

using System.ComponentModel;

User.cs

public class User : INotifyPropertyChanged
{
    private int userId;
    private string firstName;
    private string lastName;
    private string city;
    private string state;
    private string country;

    public int UserId
    {
        get
        {
            return userId;
        }
        set
        {
            userId = value;
            OnPropertyChanged("UserId");
        }
    }

    public string FirstName
    {
        get
        {
            return firstName;
        }
        set
        {
            firstName = value;
            OnPropertyChanged("FirstName");
        }
    }

    public string LastName
    {
        get
        {
            return lastName;
        }
        set
        {
            lastName = value;
            OnPropertyChanged("LastName");
        }
    }

    public string City
    {
        get
        {
            return city;
        }
        set
        {
            city = value;
            OnPropertyChanged("City");
        }
    }

    public string State
    {
        get
        {
            return state;
        }
        set
        {
            state = value;
            OnPropertyChanged("State");
        }
    }

    public string Country
    {
        get
        {
            return country;
        }
        set
        {
            country = value;
            OnPropertyChanged("Country");
        }
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    #endregion
}

Now right-click on the ViewModel folder and add a new class.

UserViewModel.cs

using System.Windows.Input;
using System.ComponentModel;
using System.Collections.Generic;

class UserViewModel
{
    private IList<User> _UsersList;

    public UserViewModel()
    {
        _UsersList = new List<User>
        {
            new User { UserId = 1, FirstName = "Raj", LastName = "Beniwal", City = "Delhi", State = "DEL", Country = "INDIA" },
            new User { UserId = 2, FirstName = "Mark", LastName = "henry", City = "New York", State = "NY", Country = "USA" },
            new User { UserId = 3, FirstName = "Mahesh", LastName = "Chand", City = "Philadelphia", State = "PHL", Country = "USA" },
            new User { UserId = 4, FirstName = "Vikash", LastName = "Nanda", City = "Noida", State = "UP", Country = "INDIA" },
            new User { UserId = 5, FirstName = "Harsh", LastName = "Kumar", City = "Ghaziabad", State = "UP", Country = "INDIA" },
            new User { UserId = 6, FirstName = "Reetesh", LastName = "Tomar", City = "Mumbai", State = "MP", Country = "INDIA" },
            new User { UserId = 7, FirstName = "Deven", LastName = "Verma", City = "Palwal", State = "HP", Country = "INDIA" },
            new User { UserId = 8, FirstName = "Ravi", LastName = "Taneja", City = "Delhi", State = "DEL", Country = "INDIA" }
        };
    }

    public IList<User> Users
    {
        get { return _UsersList; }
        set { _UsersList = value; }
    }

    private ICommand mUpdater;

    public ICommand UpdateCommand
    {
        get
        {
            if (mUpdater == null)
                mUpdater = new Updater();
            return mUpdater;
        }
        set
        {
            mUpdater = value;
        }
    }

    private class Updater : ICommand
    {
        public bool CanExecute(object parameter)
        {
            return true;
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            // Code implementation for execution
        }
    }
}

Now let's move on to View, Add a new window in View Folder.

MainPage.xaml

<Window x:Class="WpfMVVMSample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="485" Width="525">
    <Grid Margin="0,0,0,20">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <ListView Name="UserGrid" Grid.Row="1" Margin="4,178,12,13" ItemsSource="{Binding Users}">
            <ListView.View>
                <GridView x:Name="grdTest">
                    <GridViewColumn Header="UserId" DisplayMemberBinding="{Binding UserId}" Width="50"/>
                    <GridViewColumn Header="First Name" DisplayMemberBinding="{Binding FirstName}" Width="80"/>
                    <GridViewColumn Header="Last Name" DisplayMemberBinding="{Binding LastName}" Width="100"/>
                    <GridViewColumn Header="City" DisplayMemberBinding="{Binding City}" Width="80"/>
                    <GridViewColumn Header="State" DisplayMemberBinding="{Binding State}" Width="80"/>
                    <GridViewColumn Header="Country" DisplayMemberBinding="{Binding Country}" Width="100"/>
                </GridView>
            </ListView.View>
        </ListView>
        <!-- Textboxes and labels for user details -->
        <TextBox Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="80,7,0,0" Name="txtUserId" VerticalAlignment="Top" Width="178" Text="{Binding ElementName=UserGrid, Path=SelectedItem.UserId}"/>
        <!-- Other TextBoxes and Labels for User details -->
        <Button Content="Update" Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="310,40,0,0" Name="btnUpdate" VerticalAlignment="Top" Width="141" Command="{Binding Path=UpdateCommad}" />
        <!-- Textboxes and labels for user's City, State, and Country -->
        <TextBox Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="80,143,0,0" Name="txtCity" VerticalAlignment="Top" Width="178" Text="{Binding SelectedItem.City, ElementName=UserGrid}"/>
        <!-- Labels for Country, City, State -->
        <!-- Textboxes and labels for Country, City, State -->
    </Grid>
</Window>

App.xaml.cs

Now bind on application startup.

protected override void OnStartup(StartupEventArgs e)    
{    
    base.OnStartup(e);    
    WpfMVVMSample.MainWindow window = new MainWindow();    
    UserViewModel VM = new UserViewModel();    
    window.DataContext = VM;    
    window.Show();    
}  

Run the application to see the result.

Main Window

Click on row.

New Data

You can edit user details in text boxes and click the update button.

Create Data