准备好 Dialog ViewModel 绑定,调用 Dialog 并在 MVVM 中从中返回数据

发布于 2024-09-27 17:49:39 字数 1520 浏览 2 评论 0原文

您是否看到更好的方法,如何从控制器/视图模型调用/构造对话框并从中返回数据并将 DocumentViewModel 设置为对话框的 DataContext?

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

你会如何解决这种情况?使对话框正确可绑定,调用对话框并将数据从它返回到LessonPlannerController,以便新文档可以保存在数据库中并添加到绑定的文档ObservableCollection中,以便用另一个文档刷新GUI。

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... 公共字符串文档名称 {get;set;} public string keywords {get;set;}

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

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

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

Do you see a better way how I can call/contstruct a Dialog from a Controller/ViewModel return data from it and set the DocumentViewModel as DataContext of the Dialog?

The problem is I can not use View first approach in the DocumentDetailWindow and its belonging UserControl because I can not set the Model to the DocumentViewModel`s Document Property in XAML!

How would you solve that scenario? Make Dialog properly bindable, call dialog and return data from it to the LessonPlannerController so the new Document can be saved on database and added to the bound ObservableCollection of Documents so the GUI is refreshed with one more 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();
 }

UPDATE:

I have even thought about not making this => documentViewModel.Document = new Document();
because why do I need a Model in a in a ViewModel when I can just do this:

IN REALITY those properties have a NotifyPropertyChange...
public string DocumentName {get;set;}
public string Keywords {get;set;}

then I could create a Document instance with the above properties in the DocumentViewModel, when the Save command is executed and then pass the Document via Callback to the LessonPlannerControl etc... it seems View first is not working when you have to subscribe your event to a method. Only ViewModel first works then.

What do you think? Should I not use ocumentViewModel.Document = new Document();

and create those 2 properties in the DocumentViewModel. Hm... but why recreate if they are already in the Document Model?...

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文