Click here to Skip to main content
12,550,111 members (44,505 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


13 bookmarked

Using ObservableCollection in MVVM

, 21 Oct 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
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

You may also be interested in...


Comments and Discussions

GeneralMy vote of 3 Pin
Javith khan21-Oct-13 20:25
memberJavith khan21-Oct-13 20:25 
GeneralMy vote of 3 Pin
Klaus Luedenscheidt17-Oct-13 19:00
memberKlaus Luedenscheidt17-Oct-13 19:00 
GeneralRe: My vote of 3 Pin
Houssem_Dellai24-Oct-13 7:33
memberHoussem_Dellai24-Oct-13 7:33 
GeneralRe: My vote of 3 Pin
Klaus Luedenscheidt25-Oct-13 17:11
memberKlaus Luedenscheidt25-Oct-13 17:11 
GeneralRe: My vote of 3 Pin
rdh123dz18-Feb-16 23:37
memberrdh123dz18-Feb-16 23:37 
AnswerGood article houssem Keep it up Pin
Chiheb Chebbi17-Oct-13 3:36
memberChiheb Chebbi17-Oct-13 3:36 
Questionwhat is Frody extension ? Pin
Tridip Bhattacharjee16-Oct-13 21:05
memberTridip Bhattacharjee16-Oct-13 21:05 
AnswerRe: what is Frody extension ? Pin
Houssem_Dellai17-Oct-13 0:48
memberHoussem_Dellai17-Oct-13 0:48 
GeneralRe: what is Frody extension ? Pin
Tridip Bhattacharjee17-Oct-13 19:20
memberTridip 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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161021.1 | Last Updated 21 Oct 2013
Article Copyright 2013 by Houssem_Dellai
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid