How do I structure MVVM with Collections?

后端 未结 4 475
不知归路
不知归路 2021-02-01 17:45

I\'m having trouble understanding how to apply the MVVM pattern when Lists/Collections are involved.

Say the MainModel has a few properties and methods, as

4条回答
  •  清歌不尽
    2021-02-01 18:06

    You are right to have a separate DetailModels list and DetailViewModels list. The DetailViewModels list should be a property of type ObservableCollection. You can populate the observable list when you set the Model (or at construction time, if you pass the model into the constructor of your ViewModel.)

    private ObservableCollection m_details;
    public IEnumerable Details
    {
       get { return m_details; }
    }
    

    You can the subscribe to m_details.CollectionChanged. This is where you can handle re-ordering the contents of the list in the Model.

    I hope this helps.

提交回复
热议问题