Click here to Skip to main content
14,691,972 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
If I have a Model :

public class Book
{
   public string Title { get; set; }
   public List<Author> Authors { get; set; }
}

public class Author
{
   public string Name { get; set; }
}


Now my problems is how can I notify when I make changes on my model(Book) (in codebehind and not in UI)?

Lets say I changed the name of one of the author

myBook.Authors[0].Name = "New Name";


I want to change the name of the author in code behind, and the UI will reflect the change I made in the code behind.

LIke this

__________________________
Book TItle |
__________________________
New Name |
Author2 |
Author3 |
___________________________

Do I need to change the List<author> to ObservableCollection<author>?
But based on what I understand on MVVM. The Nofication must be implemented in the ViewModel.


Please help!!

Thank you.
Posted
Updated 5-Jan-13 23:47pm
v4

1 solution

You should really not be changing the value in the view model unless it is absolutely required.
Implement NotifyPropertyChanged in the view model and then set the value in the view model itself.
   
Comments
Daskul 6-Jan-13 6:10am
   
Do you mean I need to create two ViewModels? AuthorViewModel and BookViewModel that will contain ObservableCollection of AuthorViewModel?

But I want my Models be synchronized to whatever changes happened so that when I want to save my Models data lets say in a persistence storage.

How can I do that? I dont want to use the ViewModels data instead of MOdels data to be saved in persistence storage. Because base on what I understand about MVVM, ViewModels are just for supplying data on VIews and not on Business Layer.

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




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900