wpf 带有集合的 MVVM 模型。在模型中使用或不使用 observablecollection
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21976979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MVVM model with collections. Use or not observablecollection in model
提问by sexta13
I'm struggling with one point of MVVM and I hope someone can enlighten me.
我正在为 MVVM 的一点而苦苦挣扎,我希望有人能启发我。
My scenario is this: A model something like this:
我的场景是这样的:一个像这样的模型:
public class Codes
{
public string Code { get; set; }
public string Value { get; set; }
public ObservableCollection<SubCoddes> SubCodes { get; set; }
}
A ViewModel that has this:
具有以下内容的 ViewModel:
public ObservableCollection<Codes> Codes { get; set; }
The Codescollection are bound to a ListViewin the View.
该Codes集合绑定到ListView视图中的 a。
My big doubt is if there should be ObservableCollection in the model. If not, what is a better approach?
我最大的疑问是模型中是否应该有 ObservableCollection。如果没有,什么是更好的方法?
Both Codesand SubCodesare filled by a query in a MS SQLServer database when the application is initialized...and there's no option to create new ones. They can only be reused.
双方Codes并SubCodes通过在当应用程序初始化的MS SQLServer数据库查询填充......而且也没有选项来创建新的。它们只能重复使用。
I can have in Codesmultiple equal codes with different values - ex:
我可以有Codes多个具有不同值的相同代码 - 例如:
Code a = new Code { value="test1", Code ="100" }
Code b = new Code { value="test2", Code ="100" }
These values should be bound to TestBoxes in the view.
这些值应该绑定到TestBox视图中的 es。
====EDIT=====
====编辑====
Maybe I have wrongly exposed the problem. When I say cannot create more, I may have lead to a wrong preposution. What I meant is that the initial structure is created with the DB query, but in the UI there will be buttons to replicate a Code and a Subcode...and that will add those to the corresponding lists.
也许我错误地暴露了这个问题。当我说不能创造更多时,我可能会导致错误的介词。我的意思是初始结构是用 DB 查询创建的,但在 UI 中会有按钮来复制代码和子代码......这会将它们添加到相应的列表中。
Ex: in the UI:
例如:在用户界面中:
Code A - duplicate button
代码 A - 重复按钮
-> Subcode A - duplicate button
-> 子代码 A - 重复按钮
-> Subcode B - duplicate button
-> 子代码 B - 复制按钮
Code B - duplicate button
代码 B - 重复按钮
Everytime I click on a duplicate button it duplicates the structure (either subcodes or codes with subcodes).
每次我单击重复按钮时,它都会复制结构(子代码或带有子代码的代码)。
These changes must be done in the bservableCollection Codes.
这些更改必须在 bservableCollection 代码中完成。
I hope I'm making myself clear...and sorry for my English.
我希望我说清楚了……对不起我的英语。
Regards
问候
回答by markmnl
You do not need an ObservableCollection<T>that is only useful when you add or remove items from the collection after assigning it - which you say you do not. A List<T>would be fine and well within MVVM. However you still have to remember to implement INotifyPropertyChangedand raise the PropertyChangedwhen the List<T>is assigned - or anything you assign you want the binding to re-read the source.
您不需要一个ObservableCollection<T>仅在分配后从集合中添加或删除项目时才有用的东西 - 您说您不需要。AList<T>在 MVVM 中会很好并且很好。但是,您仍然必须记住在分配时实现INotifyPropertyChanged和提高- 或者您分配的任何您希望绑定重新读取源的内容。PropertyChangedList<T>
e.g. you should have:
例如你应该有:
private List<Code> codes;
public List<Codes> Codes
{
get {return codes;}
set
{
codes = value;
NotifyPropertyChanged("Codes");
}
}
private void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
The same applies to your nested collection SubCodes, i.e. Codesshould also implement the interface and the setter for SubCodes should raise the event. Furthermore if you were to modify a Code's property at runtime and you want that reflected in the UI the properties on Codeshould raise the event too. Also any class you bind to should always implement the interface even if the individual properties do not raise the event, because binding to a class that does not implement the interface creates memory leaks.(unless the Property is a DependencyProperty, which it shouldn't be unless you are authoring you own control, or the Binding Mode is OneTime).
这同样适用于您的嵌套集合 SubCodes,即Codes也应该实现接口,并且 SubCodes 的 setter 应该引发事件。此外,如果您要Code在运行时修改 a的属性,并且希望将其反映在 UI 中,则其上的属性也Code应该引发该事件。此外,您绑定到的任何类都应该始终实现接口,即使各个属性不引发事件,因为绑定到未实现接口的类会导致内存泄漏。(除非该属性是一个 DependencyProperty,它不应该是,除非您正在创作自己的控件,或者绑定模式是 OneTime)。
回答by dlac
I think the MVVM way is to have the entities/data model in the Model, and to do all the tweeks and modifications to it in the ViewModel so it can then bind it to the View. Assuming you are using observable collections to check if the user modifies some textbox maybe they should be in the ViewModel.
我认为 MVVM 的方法是在模型中包含实体/数据模型,并在 ViewModel 中对其进行所有调整和修改,以便它可以将其绑定到视图。假设您使用 observable 集合来检查用户是否修改了某些文本框,也许它们应该在 ViewModel 中。

