c# - MVVM:如何将ObservableCollection传递给其他 View 模型?

标签 c# wpf mvvm viewmodel

我正在设计一个MVVM框架,我需要知道我对MVVM模式的理解是否正确。我的问题很简单。如何在VieModels之间传递ObservableCollection对象?是不是?

我有一个CustomerViewModel,它具有一个ObservableCollection来保存客户列表。我也有一个InsertCustomerViewModel,它负责将新的客户模型插入该ObservableCollection中。在InsertCustomerViewModel中,我有一个称为Insert()的方法,该方法在用户每次单击“插入”按钮时都会被调用。

到目前为止,我正在做的是将ObsertvableCollection从CustomerViewModel传递到InsertCustmerViewModel的构造函数,然后在Insert方法中具有Items.Add(newCustomer)。

我的实现正确吗?还是有更好的方法来完成这项工作?

最佳答案

我将CustomerViewModel传递给InsertCustomerViewModel并公开集合的属性。这样,您可以直接从InsertCustomerViewModel使用和修改该集合。

关于c# - MVVM:如何将ObservableCollection传递给其他 View 模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23505916/

相关文章:

wpf - WinForms ReportViewer 挂起应用程序 WPF

wpf - 使用 MVVM 处理 SelectedItem 事件的最简单方法是什么?

wpf - 将两个 UserControl 绑定(bind)到同一个 DataContext 或 ViewModel?

c# - 将 stackPanel 的最后一项水平附加到右侧

mvvm - Xamarin表格滚动到底部

objective-c - 使用带有 MVVM 模式的 RACCommand,向 ViewModel 发送参数

c# - 我无法将数据绑定(bind)到 WPF/XAML 中的局部变量

c# - 使用 LinQ 从 IEnumerable 添加项目到列表

c# - 在 C# 中的数组上使用 Contains() 扩展方法

c# - 如何终止基于 C# 的 Excel 应用程序实例?