MVVM 和嵌套视图模型 [英] MVVM and nested view models

查看:26
本文介绍了MVVM 和嵌套视图模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的例子,我创建了一个由列表框组成的视图,列表框显示了一堆项目.我想知道我是否要在这里正确地创建 View Model 和 Model 类.在这种情况下使用正确工作的任何值,我理解这有点主观,但我当前的解决方案感觉不对.这是一个简化版本.

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 the View Model and Model classes correctly here. Use whatever value of correctly works in this context, I understand it's a bit subjective, but my current solution doesn't feel right. Here's a simplified version.

ViewModel 和模型:

The ViewModels and Models:

namespace Example
{
  public class ParentViewModel
  {
      public ParentViewModel()
      {
          // ... Create/Consume ChildViewModel * n
      }

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

  public class ChildViewModel
  {
      public ChildViewModel()
      {
          // ... Create/Consume ChildModel
      }

      public ChildModel Model { get; set; }
  }

  public class ParentModel
  {
      public List<ChildModel> ChildModels { get; set; }

      public ParentModel()
      {
          // ... Create/Consume ChildModel * n;
      }
  }

  public class ChildModel
  {
      public ChildModel()
      {
          // ... Contains actual data.
      }

      public string Data { get; set; }
  }    
}

视图:

<Window x:Class="Example.View"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:Example="clr-namespace:Example" Title="View" Height="300" Width="300"
    DataContext="{StaticResource TheViewModel}">
    <Window.Resources>
    <Example:ParentViewModel x:Key="TheViewModel" />
</Window.Resources>
<Grid>
    <ListBox Height="261" HorizontalAlignment="Left" Name="listBox1" VerticalAlignment="Top" Width="278" ItemsSource="{Binding ChildViewModels}"/>
</Grid>

在适当的代码中,列表框将使用数据模板来显示子视图模型.但是正如您所看到的,我不确定如何实例化与子相关的对象.感觉就像 ParentViewModel 将引用 ParentModel 并基于 ParentModel 的 ChildModel 对象创建 ChildViewModel 对象.现在我已经说过这听起来不那么愚蠢,但我会对你的想法感兴趣.

In the proper code, the listbox will use a data template to display the child view models. But as you can see I'm not sure how the to instantiate the child related objects. It feels like the ParentViewModel will have a reference to the ParentModel and create ChildViewModel objects based on the ParentModel's ChildModel objects. Now I've said that it doesn't sound so daft, but I'd be interested in your thoughts.

推荐答案

您走对了.

父模型自然会包含子模型列表,例如有多个订单的客户.

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

ParentViewModel 由第三方创建和加载时,它会传递一个 ParentModel.然后 ParentViewModel 将:

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

  1. ParentModel 分配给局部变量
  2. 为每个 ChildModel 创建一个 ChildViewModelChildModelChildViewModel 构造函数
  3. 将每个 ChildViewModels 添加到列表中
  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

顺便说一下,你想要

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

成为

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

这篇关于MVVM 和嵌套视图模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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