MVVM and nested view models

后端 未结 1 653
抹茶落季
抹茶落季 2020-12-13 09:42

I have a simple example where I\'m creating a View consisting of a list box, and the list box displays a bunch of items. I\'m wondering if I\'m going about the creation of t

相关标签:
1条回答
  • 2020-12-13 10:10

    You are on the right track.

    The parent model would naturally contain a list of child models, e.g. a customer having multiple orders.

    When ParentViewModel is created and loaded by a third-party, it is passed a ParentModel. Then the ParentViewModel will:

    1. Assign the ParentModel to a local variable
    2. Create a ChildViewModel for each ChildModel by passing the ChildModel to the ChildViewModel constructor
    3. Add each of those ChildViewModels to a list

    By the way, you want

    public List<ChildViewModel> ChildViewModels { get; set; }
    

    to be

    public ObservableCollection<ChildViewModel> ChildViewModels { get; set; }
    
    0 讨论(0)
提交回复
热议问题