Tutorial View without collections

This tutorial will show you how to create a view and its required classes so they correctly implement the MVVM pattern. As views showing collections need special attention this first tutorial focus on views without them. Use these steps every time you have to create a view for your project.

1 - Create view's skeleton

Create the following empty files / classes as they conform the basic MVVM structure:

  • View.xaml (the required UI)
  • ViewModel.cs (the datacontext of the view, implements INotifyPropertyChanged)
  • Model.cs (the business data shown on the UI)

The ViewModel class with have a constructor receiving a Model instance.


2 - Create the required Factories

Consider who will create the View, the ViewModel and the Model instances and who will add the references between them. Create the required code so we can create the infrastructure and open the view from within your application to be able to execute the next steps.


3 - Code the view with fixed content

Create the view that fulfills your requirements with static / fixed (faked) content so you can see how the view looks in the designer.


4 - Code the model

Create the model class that represents the business object without considering how the view will represent the data.

Think on the object you are modelling and create the appropriate properties following the standard OOP approach.

Model classes only contain data (properties) without any logic.


5 - Create ViewModel properties

Create one property in the ViewModel class for every faked value that you added to the view at Step 3 (only for faked values, not for fixed things like labels, buttons, etc.). ViewModels must be decoupled from Views so properties should not talk about view things like colors, visibilities etc. If some adaptation is required between the ViewModel and the View it has to be done using Converters.

The most common behaviour is that the property gets and sets the value from / to the model instance.


6 - Do the binding

In the View created at Step 3 replace the faked content with bindings pointing to ViewModel properties created at the previous step.

In case you need to convert your viewmodel types to do the bindings create the converters in this step too.


7 - Create Empty Commands

Create an empty class command for every action that will be started by the user from the UI.

Add one property to the ViewModel for each command class with the command type and bind the commands in the View.

There is a 'de-facto' standard about how commands look like, they all follow the pattern shown in the left image. Remember that commands can receive a parameter from the UI if needed using the CommandParameter property.


8 - Code Command Bodies

Code the command bodies so they do what the requirements specify and you will have you View completed following the MVVM pattern. Well done!

Tutorial Window with collections

1 - Header

Content


1 - Header

Content


June 17, 2014

Blog Post Title

by Start Bootstrap

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.

Read More

June 17, 2014

Blog Post Title

by Start Bootstrap

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.

Read More

June 17, 2014

Blog Post Title

by Start Bootstrap

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.

Read More

June 17, 2014

Blog Post Title

by Start Bootstrap

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.

Read More

June 17, 2014

Blog Post Title

by Start Bootstrap

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.

Read More

June 17, 2014

Blog Post Title

by Start Bootstrap

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.

Read More

Tutorial Model updated by other means

June 17, 2014

Blog Post Title

by Start Bootstrap

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.

Read More

June 17, 2014

Blog Post Title

by Start Bootstrap

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.

Read More

June 17, 2014

Blog Post Title

by Start Bootstrap

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.

Read More

June 17, 2014

Blog Post Title

by Start Bootstrap

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.

Read More