wpf - 准备好 Dialog ViewModel 绑定(bind),调用 Dialog 并在 MVVM 中从中返回数据

标签 wpf callback dialog viewmodel datacontext

您是否看到了一种更好的方法,如何从 Controller/ViewModel 调用/构造对话框并从中返回数据并将 DocumentViewModel 设置为对话框的 DataContext?

问题是我无法在 DocumentDetailWindow 及其所属的 UserControl 中使用 View 优先方法,因为我无法在 XAML 中将 Model 设置为 DocumentViewModel 的文档属性!

您将如何解决这种情况?使 Dialog 可以正确绑定(bind),调用 dialog 并将数据从它返回到 LessonPlannerController,这样新的 Document 就可以保存在数据库中并添加到绑定(bind)的 ObservableCollection of Documents 中,这样 GUI 就会用另一个 Document 刷新。

LessonPlannerController/ViewModel:

private void OnAddDocument()
  {
            DocumentDetailWindowaddDocumentWindow = new DocumentDetailWindow();
            DocumentViewModeldocumentViewModel = new DocumentViewModel();

            documentViewModel.Document = new Document();
            documentViewModel.Repository = new LessonPlannerRepository();
            documentViewModel.SaveDocumentDelegate += new Action<Document>(OnSaveDocument);

            addDocumentWindow.DataContext = documentViewModel;          
            addDocumentWindow.ShowDialog();
 }

更新:

我什至想过不做这个 => documentViewModel.Document = new Document(); 因为当我可以这样做时,为什么我需要 ViewModel 中的模型:

在现实中那些属性有一个 NotifyPropertyChange... 公共(public)字符串文件名 {get;set;} 公共(public)字符串关键字 {get;set;}

然后我可以在 DocumentViewModel 中创建一个具有上述属性的 Document 实例,当执行 Save 命令时,然后通过 Callback 将 Document 传递给 LessonPlannerControl 等...当您必须订阅时,View first 似乎不起作用你的事件到一个方法。然后只有 ViewModel 首先起作用。

你怎么看?我不应该使用 ocumentViewModel.Document = new Document();

并在 DocumentViewModel 中创建这 2 个属性。嗯...但是如果它们已经在文档模型中,为什么要重新创建呢?...

最佳答案

关于wpf - 准备好 Dialog ViewModel 绑定(bind),调用 Dialog 并在 MVVM 中从中返回数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3950359/

相关文章:

android - 在锁定屏幕上显示 Dialogfragment

c# - 如何在 wpf 的组合框中将枚举值设置为 selectedItem

javascript - 参数对应: callback vs.闭包(?)

jsf-2 - 对话框内的数据表导致宽度错误

ruby-on-rails - 我应该从模型回调还是从 Controller 发送邮件?

c - C 中的函数指针和回调

java - 无法显示安卓对话框

c# - 捕获每个 WPF MediaElement 帧

.net - 实现自定义绑定(bind)

.net - .NET Framework 4 的向后兼容性