简体   繁体   English

WPF MVVM:将项目添加到 ObservableList + DB

[英]WPF MVVM : Add item to ObservableList + DB

I have a model "Company" with "name, email, address", so I am planning to create a view with listbox to display all the companies for Database and a button to add new company(with a popup), the new company should be added to the list and to db.我有一个 model “公司”,带有“名称,email,地址”,所以我计划创建一个带有列表框的视图以显示数据库的所有公司和一个添加新公司的按钮(带有弹出窗口),新公司应该被添加到列表和数据库中。 I am confused if I have to create 2 ViewModels (one for all Companies and the other for add new company) since I have 2 views.如果我必须创建 2 个 ViewModel(一个用于所有公司,另一个用于添加新公司),我会感到困惑,因为我有 2 个视图。

so my question is what is the best approach to create a popup window for adding new company, and should I create 1 or 2 viewmodels?所以我的问题是创建弹出窗口 window 以添加新公司的最佳方法是什么,我应该创建 1 个还是 2 个视图模型?

It's not a rule to have a ViewModel to the new popup window.新弹出窗口 window 的 ViewModel 不是规则。 You can choose different approaches.您可以选择不同的方法。

Consider that one Panel in your main view is like a popup window.考虑到主视图中的一个面板就像一个弹出窗口 window。 The difference can be way the user interacts with it.区别可能在于用户与之交互的方式。

In this case you can bind your controls of the popup window to the viewmodel properties and commands.在这种情况下,您可以将弹出窗口 window 的控件绑定到视图模型属性和命令。

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

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