开发者

How to translate lists in a MVVM model to ObservableCollections in the View-Model?

开发者 https://www.devze.com 2023-01-22 16:53 出处:网络
I am writing a sample app in WPF and I\'d like the Model to be easily reusable in a WinForms app so I\'d like to keep WPF specific stuff like INotifyChanged and DependencyObjects out of it.

I am writing a sample app in WPF and I'd like the Model to be easily reusable in a WinForms app so I'd like to keep WPF specific stuff like INotifyChanged and DependencyObjects out of it.

If a Model class has a List of som开发者_JAVA技巧e other Model class, how do I implement the corresponding ObserveableCollection in the View-Model so I can keep my bindings up to date?

A use case for this would be if I have a Boss model object who has a list of Employee's. I create a Boss object, but I load the list of Employees asynchronously, how do I know when the list of Employees has been retrieved and populated? I would like to keep the loading code inside of the Model.

I suppose to summarize what I'm asking is what's the proper way to have Model to Model interactions whilst having the View-Model reflect these changes?


Your model will need to provide change notifications. You can do so in an agnostic fashion by using INotifyPropertyChanged and INotifyCollectionChanged. Contrary to your question, these are not WPF-specific interfaces. They are in System.ComponentModel - not a WPF namespace or assembly.

If you really don't want to use those interfaces, then you can always provide your own events. Whatever the case, your view model will need to attach to events to keep itself up to date with your model.

0

精彩评论

暂无评论...
验证码 换一张
取 消