5351

How to implement INotifyPropertyChanged and observableCollection in MVVM pattern?

I have an ObservableCollection of Products in the Model, and I want the ViewModel to listen to any changes in the ObservableCollection of Products.

Im not sure about how to go and implement it. Ive read several tutorials, but most of them are not MVVM specific.

Should i implement the INotifyPropertyChanged in the ViewModel class, how do I specify that i want to listen to the OberserableCollection of Products?

Thanks :)

Answer1:

I know this has already been answered, but there is more to consider.

1) a ViewModel should implement INotifyPropertyChanged, that's one of the things that makes it a ViewModel. Even in the unlikely case that the only property it exposes is the ObservableCollection, it will need to raise property changed when the actual ObservableColelction property is changed (more on this in item 3)

2) do you really want the ViewModel to listen for these changes or the View? Those are two different things. The ViewModel should hold an ObservableCollection which is then bound to the View. What you want is for the View to react to those changes. In that case, Brandon is correct that ObservableCollection does this for you out of the box. So the View has an instance of the ViewModel set as a DataContext and some visual element in your View is bound to the ObservableCollection (like an ItemsSource on a ListBox).

3) the exception is the ObservableCollection property itself in the ViewModel. While ObservableCollection implements INotifyPropertyChanged, that is part of the collection object: when that object reference changes (like it is recreated) in the ViewModel, then the ViewModel still needs to report that the Property for the ObservableCollection has changed.

Just some thoughts.

Answer2:

ObservableCollection already implements INotifyPropertyChanged, so you don't need to.

Recommend

  • I want to use Stormpath, Express.js, and Node.js. Do I want stormpath-express or express-stormpath?
  • R Tree With n Branches
  • Android multuple camera preview
  • string Multiplication in C++
  • Combine two small queries (that group by different values) into one query
  • Why does Internet Explorer sometimes trigger a local storage event before the data's ready? [du
  • Converting RVML to SVG using XSLT in C#
  • Direct insert data in mysql data in android
  • Touch events over two dom elements?
  • Maven repository location is not updated in eclipse
  • Efficiently getting XML into Elasticsearch
  • Team Foundation Server 2015 vs. Bitbucket Server (Stash)
  • How to get the url of a file of google drive to download in android
  • Is there an easy way to associate an event with a ListViewItem?
  • Typecasting `this` of a base class template to its derived class
  • Bound DataGridViewCheckBoxColumn not updating binding source
  • How do I mock an exported typescript function in a jasmine test?
  • SyntaxError: (irb):26: both block arg and actual block given
  • Salesforce Different WSDL files and when to use
  • presentShareDialogWithParams posts to FB wall, but callback handler results say error
  • ViewController With Transparent Background Entering Current ViewController With Push Transition
  • how to save the state in userdefaults of accessory checkmark-iphone
  • Time complexity of a program which involves multiple variables
  • Database structure design with variable amounts of fields
  • Checking free space on FTP server
  • How do I fake an specific browser client when using Java's Net library?
  • How to get a value (ex: baseURL) in every Karate feature?
  • recyclerView does not call the onBindViewHolder when scroll in the view
  • JSON with duplicate key names losing information when parsed
  • How to make Safari send if-modified-since header?
  • Akka Routing: Reply's send to router ends up as dead letters
  • How to pass list parameters for each object using Spring MVC?
  • Cannot Parse HTML Data Using Android / JSOUP
  • Java static initializers and reflection
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • unknown Exception android
  • JaxB to read class hierarchy
  • Checking variable from a different class in C#
  • Observable and ngFor in Angular 2
  • Unable to use reactive element in my shiny app