我应该如何将集合从一个视图的 ViewModel 传递到另一个视图?

发布于 2024-12-06 10:46:15 字数 309 浏览 0 评论 0原文

任何人都可以在以下场景中提出解决方案:

我有一个 MVVM 应用程序,我需要在主窗口中显示模式窗口,以向 viewModel 类中的 Collection 添加一些值。做到这一点的最佳方法是什么?我是说。我需要更改 viewModel 中的一些集合,我的 MainWindow 引用了 viewMode。

 viewModel = new ExamViewModel();
 this.DataContext = viewModel;

将 viewmodel 也暴露给子窗口是否足够好?或者有“正确”的方法来做到这一点。

can anyone please advise a solution in following scenario :

I have an MVVM application in which I need to show modal window from Main window to add some value to the Collection that is in viewModel class. What will be the best approach to do this. I mean. I need to change some collection in viewModel , My MainWindow have reference to viewMode.

 viewModel = new ExamViewModel();
 this.DataContext = viewModel;

Is it good enough to expose viewmodel also to child window ? Or there is "right" way to do this.

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

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

发布评论

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

评论(2

乱世争霸 2024-12-13 10:46:15

通常,模式窗口只会知道有问题的对象,允许用户填写新对象(也可能编辑现有对象)。然后它将填充的对象传递回父级,父级负责更新集合。

Normally, the modal window will only know about the object in question, allowing the user to fill in a new object (and possibly also edit an existing object). It will then pass the filled-in object back to the parent, which is responsible for updating the collection.

素衣风尘叹 2024-12-13 10:46:15

正如 @Marcelo 建议的那样,打开新子窗口的代码应该与 ViewModel 中的一些委托一起传递。该委托将创建一个子 ViewModel(例如 ChildVM),并使用其自己的集合(ParentVM.ParentCollection)填充其属性之一(例如 ChildCollection) )。

  var childVM = new ChildVM();
  childVM.ChildCollection = parentVM.ParentCollection.ToList();
  return childVM;

然后,您的子窗口将绑定到新填充的集合(ChildVM.ChildCollection)属性,并在执行“确定”/“保存”类型的确认操作后,关闭的子窗口应通知/委托回父视图模型以“合并” “更改回其旧集合...像这样...

   parentVM.ParentCollection.Clear(); 
   parentVM.ParentCollection.AddRange(ChildVM.ChildCollection);

这样

  1. 更改是在单独的列表上完成的。保持数据完整性。
  2. 只有合法的操作(确定/保存)才会合并更改。
  3. 由于断开的数据和卸载的子视图,子视图模型很容易从 UI 中拔出,并从内存中释放出来。

As @Marcelo suggested, your code that opens the new child window should be passed on with some delegate from your ViewModel. This delegate will create a child ViewModel (say ChildVM) and populate one of its properties (say ChildCollection) with its own collection (ParentVM.ParentCollection).

  var childVM = new ChildVM();
  childVM.ChildCollection = parentVM.ParentCollection.ToList();
  return childVM;

Then your child window will be bound to that newly populated collection (ChildVM.ChildCollection) property and after it performs "OK" / "SAVE" kind of affirmation actions, the closed child window should notify / delegate back to the parent viewmodel to "incorporate" changes back to its old collection... like so...

   parentVM.ParentCollection.Clear(); 
   parentVM.ParentCollection.AddRange(ChildVM.ChildCollection);

This way

  1. Changes are done on seperate lists. Data integrity is maintained.
  2. ONLY a legitimate action (OK / SAVE) merges the changes.
  3. Child viewmodel is easily plugged off the UI and and disposed off the memory due to disconnected data and the unloaded child view.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文