MVVM Basic Principals

The very basic principals so far I understand from M-V-VM practice is the following which is understood from this article :

View is a Control or Window

public partial class CustomerView : System.Windows.Controls.UserControl
public CustomerView()

And this is as far as the view might be written in code. In the most cases we don’t want to write more code in the View. The reason is that the View is supposed to be made loosely coupled from the data and the logic, probably done by creative designers.

The View gets DataContext

Some magic forces will get the data that is accessible through the ViewModel and set it to the DataContext property of the control:

protected override void OnStartup(StartupEventArgs e)

var viewModel =
new MainWindowViewModel(path);
MainWindow window =
new MainWindow();
window.DataContext = viewModel;


In this example the magic happens in the Startup method of the application. Using MEF this magic is taken out to some sophisticated standard way.
Now the View and its elements are ready to consume the data.

View binds to ViewModel

Both data and actions are binded to public accessors of ViewModel:

"2" Grid.Column="2"
"{Binding Path=FirstName, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"

"{Binding Path=SaveCommand}"

ViewModel is unaware of View

Unlike the Presenter in MVP, a ViewModel does not need a reference to a view. This makes it possible to attach any View at any time to consume the public data provided by ViewModel.

ViewModel exposes public properties to share data with any possible View:

public class CustomerViewModel : WorkspaceViewModel, IDataErrorInfo
public string FirstName
get { return _customer.FirstName; }
if (value == _customer.FirstName)

_customer.FirstName = value;


ViewModel exposes public commands to share actions with any possible View:

/// <summary>
/// Returns a command that saves the customer.
/// </summary>
public ICommand SaveCommand
if (_saveCommand == null)
_saveCommand =
new RelayCommand(
param =>
param =>
return _saveCommand;

Some of these properties may be data coming from a model and some might be simply a state of the ViewModel to help out the View.

Data Manipulation

The ViewModel, never the View, performs all modifications made to the model data.
As I already mentioned the ViewModel is unaware of View but it is certainly aware of Model and can manipulate it when needed. When user interacts with the View, the binding manager will take care of both ways of data modifications in each binding.

The way a ViewModel is attached to a model, is an architectural decision and makes your application easier or hard to understand and maintain. But this is another important subject. For now, like MEF does the magic for coupling View and ViewModel, there needs to be some Factory or Controller mechanism to pull the data and assign it to the ViewModel.

Author: Pouya Panahy

Microsoft certified DevOps engineer with passion in analysing, designing and implementing solutions for Azure Cloud with hands-on experience in security and quality assurence.

Leave a Reply