Model MVVM with a list?

I have a silverlight wp7 application and am using the MVVM pattern. The model contains a list of classes (the class type encapsulates the data of the stock ticker), and this list is used in the view along with the element template.

I understand that for this list to work with Binding, it must be an observable collection (raising the PropertyChanged event does not seem to do anything).

Thus, how can I maintain the decoupling between my model, which must be agnostic for presentation, and the presentation, which requires an observable collection?

Currently, in my View model, I am dynamically creating an observable collection using the data in model.List. It looks like a scrap.

+4
source share
2 answers

There are only two ways to do this:

  • Ask the model to set an ObservableCollection . This means that you will not need kludge in your ViewModel, but you can say that this simply moves the problem elsewhere.

  • Imagine that the ViewModel provides the ObservableCollection object that it creates, as it is now. If the collection can be updated (which is not the case in your scenario), the ViewModel should also โ€œtake ownershipโ€ of the Model and propagate the changes back to the List .

I do not believe that you are doing this kludge. The model is, of course, completely separate from the presentation (View), because it is a ViewModel, which needs all the problems to make everything work. This is fully consistent with the spirit of MVVM.

+3
source

I agree that this is not a kludge, and for practical purposes, when your model is an observable collection, this is the best solution, especially if you are going to update it from external sources.

The other two parameters use a relay command from any source that can update the list to let it know that it needs to re-synchronize, or add a handler in your view model to an event with a list item that will then add this item to your observed collection in your vm.

+1
source

Source: https://habr.com/ru/post/1342389/


All Articles