Click here to Skip to main content
15,888,733 members
Articles / Desktop Programming / XAML
Tip/Trick

Using ObservableCollection in MVVM

Rate me:
Please Sign up or sign in to vote.
4.13/5 (7 votes)
21 Oct 2013CPOL2 min read 47K   13   9
Reduce the number of properties in the View-Model by using ObservableCollection

Introduction

MVVM is a design pattern for XAML based applications. It ensures separation of concerns, test-ability and maintainability. The viewmodel should contain all the properties that will be bound to the view. Almost every element you want to bind to the view, you have to add a property in the viewmodel to control it, even if you want to insert a loading wheel. That's very powerful thanks to the Binding mechanism, especially with the use of notification with INotifyPropertyChanged interface.

Problem

However, the drawback of this pattern is that the number of properties will be high especially when you have a rich view. This will make your viewmodel look too long. Consider that for every property, you will need almost the following 6 lines of code:

C#
private string _name;
public String Name
{
    get { return _name; }
    set { Set(ref _name = value); }
}

Imagine if you have only 10 properties in each viewmodel. Then, you will have 60 lines of code only for properties!

Solution

A first solution is to use the Fody extension to decorate a property with an attribute. This will enable notification of the bounded property without using a backed field or implementing the INotifyPropertyChanged. Fody will inject the needed code at compile-time. Then you will have only 2 lines of code for each one:

C#
[AlsoNotifyFor("Name")]
public String Name { get; set; }  

Let's make it even better and reduce the 20 lines to only 1 line. Is that possible? Yes! All we need is to have an object that can contain many objects with different types and each one is notifiable. Fortunately, the ObservableCollection<object> do. Consider using the " = " operation than the Add(..) method, because Add doesn't enable notification. To face this detail, Windows Store 8.1 templates (Hub app and GridView app) comes with an object called ObservableDictionary. Which is also good in that it uses the key string to identify a value rather than a number.

Implementation

C#
public ObservableDictionary Model { get; set; }
Model = new ObservableDictionary(); 
Model.Add("Fullname", "Houssem Dellai");

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Microsoft
Tunisia Tunisia
I'm a Software Engineer and MVP (Client Development). I like writing articles and developing open source software in C#, Windows Phone and Windows 8 applications droidcon.tn/speakers.php.
http://houssemdellai.net

Comments and Discussions

 
GeneralMy vote of 3 Pin
Javith khan21-Oct-13 20:25
Javith khan21-Oct-13 20:25 
My Vote of 3
GeneralMy vote of 3 Pin
Klaus Luedenscheidt17-Oct-13 19:00
Klaus Luedenscheidt17-Oct-13 19:00 
GeneralRe: My vote of 3 Pin
Houssem_Dellai24-Oct-13 7:33
Houssem_Dellai24-Oct-13 7:33 
GeneralRe: My vote of 3 Pin
Klaus Luedenscheidt25-Oct-13 17:11
Klaus Luedenscheidt25-Oct-13 17:11 
GeneralRe: My vote of 3 Pin
rdh123dz18-Feb-16 23:37
rdh123dz18-Feb-16 23:37 
AnswerGood article houssem Keep it up Pin
Chiheb Chebbi17-Oct-13 3:36
Chiheb Chebbi17-Oct-13 3:36 
Questionwhat is Frody extension ? Pin
Tridip Bhattacharjee16-Oct-13 21:05
professionalTridip Bhattacharjee16-Oct-13 21:05 
AnswerRe: what is Frody extension ? Pin
Houssem_Dellai17-Oct-13 0:48
Houssem_Dellai17-Oct-13 0:48 
GeneralRe: what is Frody extension ? Pin
Tridip Bhattacharjee17-Oct-13 19:20
professionalTridip Bhattacharjee17-Oct-13 19:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.