WPF:如何在单击按钮时关闭对话框

发布于 2024-10-19 11:37:38 字数 499 浏览 1 评论 0原文

所以我有一个可编辑的 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 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

々眼睛长脚气 2024-10-26 11:37:38

我在这里看到两种方法:

1) EditorVM 对对话框一无所知。在这种情况下,我会将此虚拟机保留为一个简单的编辑器,它公开属性但没有 SaveCommand。然后Save按钮应该从外部注入,在你打开对话框的地方。然后在关闭对话框后,检查对话框是使用保存按钮关闭还是使用取消按钮关闭。然后,打开对话框的代码应该检查返回的结果,如果使用 Save 按钮关闭对话框,则调用 Save 方法。使用这种方法,您必须使对话框变得更加复杂 - 您必须从 ShowDialog 方法插入 SaveCancel 按钮将它们作为将要显示的 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 no SaveCommand. Then Save button should be injected from outside, somewhere where you open a dialog. Then after closing dialog you check whether dialog was closed using Save button or it was closed using Cancel button. Then code which opened a dialog should check returned result and invoke Save method if dialog was closed with Save button. With this approach you will have to make your dialog a little bit more complicated - you will have to insert Save and Cancel buttons from ShowDialog methods instead of pulling them as part of ViewModel which will be displayed. But this approach allows isolating your EditorVM from any dialog-specific behaviors.

2) EditorVM assumes that it was opened in dialog and in Save command handler it saves it's content and closes the dialog. In order to access dialog's functionality, I'm usually using some kind of IWindowManager service which handles all opened windows and can determine which dialog contains which viewModel and can close them accordingly. Basically this IWindowManager service has method like void CloseDialog(object ViewModel);.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文