MVVM - VM的集合 [英] MVVM - Collections of VMs

查看:90
本文介绍了MVVM - VM的集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相当典型的场景,即模型A有一组模型B.我还有两个模型的VM和A和VM B.问题是,如何在VM A中公开VM B的集合。我做了很多谷歌搜索,我发现
的最佳帖子是:
https://stackoverflow.com/questions/15830008/mvvm-and-collections-of-vms
看起来很有趣但是如何你真的使用它,特别是需要传递给构造函数的IViewModelProvider。

I have a rather typical scenario, i.e. a model A that has a collection of models B. I also have a VM A and VM B for the two models. The question is, how do I expose the collections of VM Bs in VM A. I did a lot of googling and the best post I found was this: https://stackoverflow.com/questions/15830008/mvvm-and-collections-of-vms That looks pretty interesting but how do you actually use this, in particular the IViewModelProvider that needs to be passed to the constructor.

我也对如何实现它的任何其他建议感兴趣。

I am also interested in any other suggestions how to implement this.

推荐答案

您好hfaun,

Hi hfaun,

>> 问题是,如何在VM A中公开VM B的集合。 

你呢想得到这个:

 //Course (Model)
    public class Course
    {
        public ObservableCollection<Student> Students { get; set; }
        //...
    }

    //Student (Model)
    public class Student
    {
        public string Name { get; set; }
        public int Grade { get; set; }
        //...
    }

    //StudentViewModel
    public class StudentViewModel
    {
        Student _model;

        public string Name
        {
            get { return _model.Name; }
            set
            {
                if (value == _model.Name) return;

                _model.Name = value;
                base.OnPropertyChanged("Name");
            }
        }
        //...
    }

    //CourseViewModel
    public class CourseViewModel
    {
        public ObservableCollection<StudentViewModel> Students { get; set; }
      
    }

最好的问候,

Cherry


这篇关于MVVM - VM的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆