简体   繁体   English

如何使用 MVVM 架构在 WPF 中实现简单的树数据结构?

[英]How to implement simple tree data structure in WPF using MVVM architecture?

New to MVVM architecture;新的 MVVM 架构; I've been looking at this article but I'm not sure what pieces of code belong in model or viewmodel.我一直在看这篇文章,但我不确定哪些代码属于模型或视图模型。 Can someone show me how its done?有人可以告诉我它是怎么做的吗? Also, does every model need a raisepropertychanged() function?另外,每个模型都需要raisepropertychanged()函数吗?

Thank you for your answers in advance!提前感谢您的回答!

Somewhat general answer here but:这里有些笼统的答案,但是:

Viewmodels should contain the information needed for the associated view and handle view interaction with the model.视图模型应该包含关联视图所需的信息并处理视图与模型的交互。 This includes data to fill things like textboxes and functions that determine what to do when a button is pressed (a command).这包括填充文本框之类的数据和确定按下按钮(命令)时要执行的操作的函数。

The model is everything not directly connected to a view.模型是所有不直接连接到视图的东西。 This might be custom data types or classes for interacting with a database.这可能是用于与数据库交互的自定义​​数据类型或类。

The model should not have a raisepropertychange() function - more accurately it should not implement the INotifyPropertyChanged interface.该模型不应具有 raisepropertychange() 函数 - 更准确地说,它不应实现 INotifyPropertyChanged 接口。 Every viewmodel should implement INotifyPropertyChanged, the easiest way to do this is generally to have a base viewmodel class that all of your viewmodels inherit from which implements the interface.每个视图模型都应该实现 INotifyPropertyChanged,最简单的方法通常是拥有一个基础视图模型类,所有视图模型都继承自它实现接口。

Here is a good example of the INotifyProperty implementation.这是INotifyProperty 实现的一个很好的例子。 , I personally use the last option under the C#7 heading in my viewmodel base class and call the SetField() function from each property. ,我个人在我的视图模型基类中使用 C#7 标题下的最后一个选项,并从每个属性调用 SetField() 函数。

If you REALLY want to learn MVVM from scratch, try this article (3 parts) .如果你真的想从头开始学习 MVVM,试试这篇文章(3 部分) From here you will get the big picture .从这里你会得到大局 There is explained what is a model, what is a view model, what is a view.解释了什么是模型,什么是视图模型,什么是视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM