Click here to Skip to main content
14,447,974 members

Using ObservableCollection in MVVM

Rate this:
4.13 (7 votes)
Please Sign up or sign in to vote.
4.13 (7 votes)
21 Oct 2013CPOL
Reduce the number of properties in the View-Model by using ObservableCollection


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.


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:

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!


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:

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.


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


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


About the Author

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

Comments and Discussions

GeneralMy vote of 3 Pin
Javith khan21-Oct-13 21:25
MemberJavith khan21-Oct-13 21:25 
GeneralMy vote of 3 Pin
Klaus Luedenscheidt17-Oct-13 20:00
MemberKlaus Luedenscheidt17-Oct-13 20:00 
GeneralRe: My vote of 3 Pin
Houssem_Dellai24-Oct-13 8:33
MemberHoussem_Dellai24-Oct-13 8:33 
GeneralRe: My vote of 3 Pin
Klaus Luedenscheidt25-Oct-13 18:11
MemberKlaus Luedenscheidt25-Oct-13 18:11 
GeneralRe: My vote of 3 Pin
rdh123dz19-Feb-16 0:37
Memberrdh123dz19-Feb-16 0:37 
AnswerGood article houssem Keep it up Pin
Chiheb Chebbi17-Oct-13 4:36
MemberChiheb Chebbi17-Oct-13 4:36 
Questionwhat is Frody extension ? Pin
Tridip Bhattacharjee16-Oct-13 22:05
professionalTridip Bhattacharjee16-Oct-13 22:05 
AnswerRe: what is Frody extension ? Pin
Houssem_Dellai17-Oct-13 1:48
MemberHoussem_Dellai17-Oct-13 1:48 
GeneralRe: what is Frody extension ? Pin
Tridip Bhattacharjee17-Oct-13 20:20
professionalTridip Bhattacharjee17-Oct-13 20: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.

Posted 16 Oct 2013

Tagged as


13 bookmarked