gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 23:55:48 27 4
gpt4 key购买 nike

您是否看到了一种更好的方法,如何从 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com