MVVM, View Model (VM) acts as a glue and controller between View and Model. It also interacts with those libraries and services that help in CRUD (Create, Retrieve, Update, and Delete) operations on the Model.
In this recipe we will implement a View Model for our Model class, User
. The View Model will also contain logic to pass data to and receive data from View. It will also interact with the data repository we implemented in our previous recipe.
Launch Visual Studio 2012 and open
WpfMVVM.sln
.Add a new folder to the
WpfMVVM
project. Name itView Model
.Add a new class to the
View Model
folder. Name itUserViewModel
.Add a reference to the Model project.
Add a private variable of type
IDataRepository
. Name it_repository
:private IDataRepository _repository;
Add another private variable of type collection of
User
. Name it_users
:private ObservableCollection<User> _users;
Next, add a no-argument/default constructor. Set
_repository
as the new instance...