WPF:如何在单击按钮时关闭对话框
所以我有一个可编辑的 ViewModel,我不希望它只能通过对话框窗口进行编辑。
通常,ViewModels 视图仅显示数据,但您可以按“编辑”按钮,通过调用 window.showDialog() 打开一个新的 Xaml 窗口。该窗口接受 viewModel 作为 DataContext,将其属性公开为可编辑,并将“保存”和“取消”命令绑定到按钮。
一切正常,但我遇到了一些问题。
首先,保存命令可以工作,但不会关闭对话框。其次,它有点破坏了 MVVM,因为 VM 必须知道 EditDialog 视图才能创建它。
当我单击 X 关闭按钮时也会发生什么。我知道 Dialog 通常会返回 false 作为 DialogResult 但在这里我不处理结果。
有没有人用 MVVM 做过类似但优雅的事情?
编辑
我还注意到,如果我使用 .ShowDialog,即使我公开为文本框,我也无法编辑任何内容。这是因为模态对话框应该如何工作吗?
So I have a editable ViewModel where I wan't it only to be editable through a Dialog window.
Normally the ViewModels view only shows the data but you could press a Edit button which opens up a new Xaml window by calling window.showDialog(). The window takes in the viewModel as it's DataContext, exposes it's properties as editable and has the Save and Cancel commands bound to buttons.
It all works fine but I'm having some problems with this.
Firstly the save command works but it doesn't close the dialog. Secondly it kind of breaks the MVVM because the VM has to know of the EditDialog view to create it.
Also what happens when I click the X close button. I know a Dialog would normally return false as DialogResult but here I'm not handling results.
Has anyone done something similar but elegantly using MVVM?
EDIT
I also noticed that if I use .ShowDialog I can't edit anything even if I expose as TextBoxes. Is this because of how Modal Dialogs are supposed to work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在这里看到两种方法:
1)
EditorVM
对对话框一无所知。在这种情况下,我会将此虚拟机保留为一个简单的编辑器,它公开属性但没有SaveCommand
。然后Save
按钮应该从外部注入,在你打开对话框的地方。然后在关闭对话框后,检查对话框是使用保存
按钮关闭还是使用取消
按钮关闭。然后,打开对话框的代码应该检查返回的结果,如果使用Save
按钮关闭对话框,则调用Save
方法。使用这种方法,您必须使对话框变得更加复杂 - 您必须从ShowDialog
方法插入Save
和Cancel
按钮将它们作为将要显示的ViewModel
的一部分进行拉取。但这种方法允许将您的EditorVM
与任何特定于对话框的行为隔离。2)
EditorVM
假定它是在对话框中打开的,并且在Save
命令处理程序中保存其内容并关闭对话框。为了访问对话框的功能,我通常使用某种IWindowManager
服务来处理所有打开的窗口,并可以确定哪个对话框包含哪个 viewModel 并可以相应地关闭它们。基本上这个IWindowManager
服务具有类似void CloseDialog(object ViewModel);
的方法。I see two ways here:
1)
EditorVM
doesn't know anything about dialog. In this case I would leave this VM as a simple editor which exposes properties but has noSaveCommand
. ThenSave
button should be injected from outside, somewhere where you open a dialog. Then after closing dialog you check whether dialog was closed usingSave
button or it was closed usingCancel
button. Then code which opened a dialog should check returned result and invokeSave
method if dialog was closed withSave
button. With this approach you will have to make your dialog a little bit more complicated - you will have to insertSave
andCancel
buttons fromShowDialog
methods instead of pulling them as part ofViewModel
which will be displayed. But this approach allows isolating yourEditorVM
from any dialog-specific behaviors.2)
EditorVM
assumes that it was opened in dialog and inSave
command handler it saves it's content and closes the dialog. In order to access dialog's functionality, I'm usually using some kind ofIWindowManager
service which handles all opened windows and can determine which dialog contains which viewModel and can close them accordingly. Basically thisIWindowManager
service has method likevoid CloseDialog(object ViewModel);
.