尝试使用嵌套视图和嵌套ViewModel创建MVVM ViewModelFirst

时间:2015-12-09 04:26:18

标签: wpf mvvm view-model-pattern

我是使用MVVM的新手,我正在尝试使用ViewModel First策略找到一个相当优雅的方法,同时创建嵌套视图的界面。我已经采取了一种蛮力的方法来制定可行的解决方案,但我无法想象这是应对这一挑战的理想方式。我在任何论坛都没有找到解决方案。我已经包含了一些有限的函数伪造类和代码来展示我的工作理念。任何帮助将不胜感激。

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows.Controls;


public class BogusStart
{
    public static Lesson GetLesson(int id)
    {
        //entity and children would be called from repository
        //return LessonRepository.Get(id)
        //.Include("Activity)
        //.Include("Activity.SubActivity)
        //.FirstorDefault();

        //Dummy Return
        return new Lesson();
    }
    public UserControl BuildLessonAndChildViews()
    {
        var _LessonViewModel = LessonViewModel.ViewModelFactory(GetLesson(1));
        var _LessonView = new LessonView();
        foreach (ActivityViewModel _ActivityViewModel in _LessonViewModel.ActivityViewModels)
        {
            var _ActivityView = new ActivityView();
            _ActivityView.DataContext = _ActivityViewModel;

            foreach (SubActivityViewModel _SubActivityViewModel in _ActivityViewModel.SubActivityViewModels)
            {
                var _SubActivityView = new SubActivityView();
                _SubActivityView.DataContext = _SubActivityViewModel;
                _ActivityView.ChildrenSP.Children.Add(_SubActivityView);
            }
            _LessonView.ChildrenSP.Children.Add(_ActivityView);
        }

        return _LessonView;


    }

}
public class Lesson
{
    public int id { get; set; }
    public string Title { get; set; }
    public string Desc { get; set; }
    public ICollection<Activity> Activities { get; set; }
}

public class Activity
{
    public int id { get; set; }
    public string Title { get; set; }
    public string Desc { get; set; }
    public int ScoreWeight { get; set; }
    public ICollection<SubActivity> SubActivities { get; set; }
}
public class SubActivity
{
    public int id { get; set; }
    public string Title { get; set; }
    public string Desc { get; set; }
    public int ScoreWeight { get; set; }
}


public class LessonViewModel
{
    public Lesson _Lesson { get; set; }
    public ObservableCollection<ActivityViewModel> ActivityViewModels { get; set; }
    public LessonViewModel(Lesson _lesson)
    {
        _Lesson = _lesson;


        foreach (Activity _Activity in _Lesson.Activities)
        {
            ActivityViewModels.Add(ActivityViewModel.ViewModelFactory(this, _Activity));
        }
    }
    public static LessonViewModel ViewModelFactory(Lesson _lesson)
    {
        return new LessonViewModel(_lesson);
    }
}
public class ActivityViewModel
{
    public Activity _Activity { get; set; }
    public LessonViewModel _LessonViewModel { get; set; }
    public ObservableCollection<SubActivityViewModel> SubActivityViewModels { get; set; }
    public ActivityViewModel(LessonViewModel _lessonViewModel, Activity _activity)
    {
        _Activity = _activity;
        _LessonViewModel = _lessonViewModel;

        foreach (SubActivity _subActivity in _Activity.SubActivities)
        {
            SubActivityViewModels.Add(SubActivityViewModel.ViewModelFactory(this, _subActivity));
        }
    }

    public static ActivityViewModel ViewModelFactory(LessonViewModel _lessonViewModel, Activity _Activity)
    {
        return new ActivityViewModel(_lessonViewModel, _Activity);
    }

}
public class SubActivityViewModel
{
    public SubActivity _SubActivity { get; set; }
    public ActivityViewModel _ActivityViewModel { get; set; }
    public SubActivityViewModel(ActivityViewModel _activityViewModel, SubActivity _subActivity)
    {
        _ActivityViewModel = _activityViewModel;
        _SubActivity = _subActivity;
    }
    public static SubActivityViewModel ViewModelFactory(ActivityViewModel _activityViewModel, SubActivity _subActivity)
    {
        return new SubActivityViewModel(_activityViewModel, _subActivity);
    }
}

///example of simple format for each view control
/// <Grid>
//    <StackPanel x:Name="MainSP" Orientation="Vertical">
//        <TextBlock Text = "{Binding id}" ></ TextBlock >
//        < TextBlock Text="{Binding Title}"></TextBlock>
//        <TextBlock Text = "{Binding Desc}" ></ TextBlock >
//    </ StackPanel >
//    < StackPanel x:Name="ChildrenSP" Orientation="Vertical"></StackPanel>
//</Grid>

}

0 个答案:

没有答案