如何在 ViewModel 中公开模型集合

发布于 2024-11-28 10:43:34 字数 452 浏览 0 评论 0原文

我正在为一个表示递归嵌套类型的类实现 MVVM。例如:

class NestedType
{
    // Other properties here

    public Collection<NestedType> SubElements {get; set;}
}

class NestedTypeViewModel
{
    ObservableCollection<NestedType> ModelCollection { ??? }
}

如何以可观察的方式公开模型集合中的项目? (即,视图将添加、创建和修改子元素)我假设我需要 ViewModel 中的集合为 ObservableCollection 但在 Model 中呢?我还可以将其设为 ObservableCollection 并直接公开它...

有什么建议吗?

I'm implementing MVVM for a class which represents recursively nested types. For example:

class NestedType
{
    // Other properties here

    public Collection<NestedType> SubElements {get; set;}
}

class NestedTypeViewModel
{
    ObservableCollection<NestedType> ModelCollection { ??? }
}

How do I expose the items in the Model's collection in an observable way? (i.e., the View will be adding, creating, and modifying the subelements) I assume I need the collection in the ViewModel to be ObservableCollection<T> but what about in the Model? I could also make that an ObservableCollection and just directly expose it...

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尸血腥色 2024-12-05 10:43:34

您可以直接从模型返回 ObservableList。 WPF 将获取 Collection 的引用并注册到模型集合的 CollectionChanged 事件。

class MyViewModel
{
  public ObservableCollection<NestedType> MyItems { return Model.Items; }
} 

但是,您将破坏使用 MVVM 想要实现的目标。 ViewModel 不应公开模型、单个模型或集合,否则您的视图可能会绑定到模型,而您不应该在 MVVM 中这样做。

MVVM 不是宗教,每个人以不同的方式实现某些部分。如果这对你来说没问题……那就直接做吧。

如果您想要一个干净的解决方案,您有多种选择。只需允许管理器修改模型的集合并创建相应的视图模型,或者我最喜欢的方法是拥有一个同步模型和视图模型集合的对象,通过自动将新添加的模型包装在新的视图模型中并将此虚拟机添加到父视图模型列表。

You can directly return the ObservableList from your model. WPF will take the reference of the Collection and registers to the CollectionChanged Event of the model collection.

class MyViewModel
{
  public ObservableCollection<NestedType> MyItems { return Model.Items; }
} 

BUT, you will break what you are trying to achieve by using MVVM. A ViewModel should not expose a model, single or collection, otherwise your view could bind to a model which you should not do in MVVM.

Well MVVM is no religion and everyone implements certain parts differently. If this is no problem for you ... Go ahead and do it directly.

If you want a clean solution, you have multiple options to go from here. Just allowing a Manager to modify the collection of the model and create a corresponding view model or My favorit approach is to have an object that syncs model and viewmodel collection, by automatically wrap newly added models in a new view model and add this vm into the parent viewmodel list.

烂柯人 2024-12-05 10:43:34

只要您不需要将不同的 Models 传递到 ViewModel 中,您就可以没有单独的 Model 实体,因此您可以坚持使用 >ViewModel 代表实际的Model 数据。

我建议在引擎盖下使用私有字段以及 INotifyPropertyChanged 来公开它:

class NestedTypeViewModel 
{     
    private ObservableCollection<NestedType> dataItems; 

    public ObservableCollection<NestedType> DataItems
    {
       get
       { 
          return this.dataItems;
       }
       private set
       {
           this.dataItems = value;
           // here is should be OnPropertyChanged("DataItems") call
       }
    } 
} 

编辑:评论的答案

但是如果您已经拥有像 Model 这样的实体 - 那么请尝试注入ViewModel 通过接口,因此 Model 和 ViewModel 至少会解耦

You can live without separate Model entity as long as you don't need to pass different Models into ViewModel, so you can stick with ViewModel which represents an actual Model data.

I would recommend to expose it using private field under the hood along with INotifyPropertyChanged:

class NestedTypeViewModel 
{     
    private ObservableCollection<NestedType> dataItems; 

    public ObservableCollection<NestedType> DataItems
    {
       get
       { 
          return this.dataItems;
       }
       private set
       {
           this.dataItems = value;
           // here is should be OnPropertyChanged("DataItems") call
       }
    } 
} 

EDIT: answer to comments

But if you already has such entity like Model - so try to inject in ViewModel by an Interface so Model and ViewModel at least would be decoupled

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文