准备好 Dialog ViewModel 绑定,调用 Dialog 并在 MVVM 中从中返回数据
您是否看到更好的方法,如何从控制器/视图模型调用/构造对话框并从中返回数据并将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这些能回答你的问题吗?
WPF MVVM 对话框示例
或者
http://www.codeproject.com/KB/architecture/MVVM_Dialogs.aspx
Do these answer your question?
WPF MVVM dialog example
or
http://www.codeproject.com/KB/architecture/MVVM_Dialogs.aspx