Examples View without collections

The following example will show you how to execute the tutorial steps when creating a view. The requirement is to have a view to enter people information to a database. The view has to allow the user to enter the name, the address, the gender and the birth date. To simplify data validation birth date has to be entered using comboboxes. There will be an image next to the gender selection showing a male / female image. There will be a button to save the data and another one to erase all the data.

1 - Create view's skeleton

This step is pretty straightforward, we just need to create the empty files paying attention to the following details:

  • The View contains the design datacontext pointing to a DesignInstance of the ViewModel.
  • The ViewModel has a constructor receiving the model.
  • It can happen that the Model class exists when you reach this step because it was created during other developments, if that's the case simply use it.

Of course rename the files to suit your project needs. My recommendation is to have the view and the viewmodel ending with View and ViewModel.


2 - Create the required Factories

In this step we have to code the factories that will create everything.

As the factory has to create several object instances and return them we created a class (ViewInfrastructure) containing these instances so the factory's Create() method returns it so we avoid using out parameters.

Avoid the temptation of skipping this step, having a factory to create the objects follows the Single Responsibility principle and give us the change of having all the view's creation code together.

Remember to add the code that calls the factory in this step as well, we have added it to the App.xaml.cs but you will add it normally inside a command opening the View or a similar place.


3 - Code the view with fixed content

In this step we only need to modify the View.xaml file and it is as simple or as complex as the view requirements are.

The example requirements are fairly simple so we simply implemented them. No bindings at all, only fixed content.


4 - Code the model

If the Model was created in Step 1 it is time to fill it. If you are using an existing class skip this step (or use it to verify that is correctly designed).

Remember that Model classes only contain data, not behaviour, that's why these classes should contain only properties.

In fact this step is not related with MVVM at all but with OOP design. Think on the object you are modelling and the properties you need from it, do not model unneeded properties. So the first thing is to find out the Model properties and their names. Then it is time to think on properties types, think on the datatype that best represents the property value.

Talking about the example, its properties are pretty clear, the only controversy could be around the BirthDay property. In this step we must skip the View requirements at all and think on the class being modelled. A Person has a BirthDate and a Date is represented with a DateTime, that's why we model it like this.


5 - Create ViewModel properties

Now we have to code the ViewModel. Our View has 9 fixed values but 4 of them depend on the same data (Male or Female) so we need 5 properties in the ViewModel.

The ViewModel converts the data between the View and the Model so the properties normally retrieve / set / adapt the value from / to it.

The ViewModel has to be view agnostic so no View concepts / types should appear on it, that's why we do not put properties in the ViewModel with type Visibility to show / hide the person image. Instead of that we will use Converters. This will decouple the ViewModel from the View making possible to replace the View with a different one.


6 - Do the binding

In this step we will replace in the View all the fixed contents with binding to ViewModel properties we created in the previous step. We have to create the required converters too.

In this example we need 3 different converters:

  • A boolean to visibility converter (BooleanToVisibilityConverter) which is part of .Net framework.
  • A negated boolean converter (negates a boolean value coming from the ViewModel).
  • A negated visibility converter (returns the opposite value of the BooleanToVisibilityConverter).

It is preferred to use converters over adding negated values to the ViewModel because we need the values negated because the way the View is done (it uses radiobuttons) so adding these values to the ViewModel couples it to the View implementation.


7 - Create Empty Commands

Having all the data in place now it is time to code the behaviours using commands. You have to create a command for every action done in the View, as we have two we created two empty commands containing a DelegateCommand class. It is a common ICommand implementation, it can be found in the Prism package. The source code is available as well in the example if you do not want to add that reference.

After creating the commands we add them to the ViewModel and modify the factory created in the Step 2 (remember we said it was worth it?) so the ViewModel receives them in the constructor.

Finally we add the command bindings to the View so its buttons use the new commands.


8 - Code Command Bodies

We are almost there! We only need to code the bodies of the command's Execute methods and add to the commands whatever dependencies they have.

You will find frequently a cross dependence between the commands and the viewmodels as the ViewModel contains the commands but the commands update the ViewModel. Ideally the commands should receive their dependencies in the constructor but as in this case is not possible the ViewModel is added as a property and set in the factory.

Examples Window with collections

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

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

Examples 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