如何按照 MVVM 为 WPF 构建通用/可重用模式对话框
我想构建一个通用/可重用的模式对话框,可以在我们的 WPF (MVVM) - WCF LOB 应用程序中使用。
我有一个视图和关联的视图模型,我想使用对话框显示它们。视图和视图模型之间的绑定是使用针对类型的数据模板完成的。
以下是我能够起草的一些要求:
- 我更喜欢它基于窗口,而不是使用像模式对话框一样的装饰器和控件。
- 它应该从内容中获得最小大小。
- 它应该以所有者窗口为中心。
- 窗口不得显示最小化和最大化按钮。
- 它应该从内容中得到标题。
最好的方法是什么?
I would like to build a generic/re-usable modal dialog that I can use in our WPF (MVVM) - WCF LOB application.
I have a Views and associated ViewModels that I would like to display using dialogs. Bindings between Views and ViewModels are done using Type-targeted DataTemplates.
Here are some requirements that I have been able to draft:
- I prefer this to be based on a Window instead of using Adorners and controls that act like a modal dialog.
- It should get its minimum size from the content.
- It should center on the owner window.
- The window must not show the Minimize and Maximize buttons.
- It should get its title from the content.
What is the best way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我通常通过将此接口注入适当的 ViewModel 来处理此问题:
这允许 ViewModel 生成子窗口并在无模式下以模态方式显示它们。
IWindow 的可重用实现是这样的:
您可以使用此 Window 作为可重用宿主窗口。没有隐藏代码:
您可以在我的书。
I usually deal with this by injecting this interface into the appropriate ViewModels:
This allows the ViewModels to spaw child windows and show them modally on modeless.
A reusable implementation of IWindow is this:
You can use this Window as a reusable host window. There's no code-behind:
You can read more about this (as well as download the full code sample) in my book.
我正在回答我自己的问题,以帮助其他人找到我在一个地方努力找到的所有答案。上面看起来像是一个简单的问题,实际上提出了多个问题,我希望在下面能够充分回答这些问题。
就这样吧。
将用作通用对话框的 WPF 窗口可能如下所示:
按照 MVVM,显示对话框的正确方法是通过中介器。要使用中介器,您通常还需要一些服务定位器。有关调解器的具体详细信息,请查看此处。
我选择的解决方案涉及实现一个 IDialogService 接口,该接口通过一个简单的静态 ServiceLocator 来解析。 这篇优秀的 codeproject 文章对此有详细介绍。请注意此文章论坛中的留言。该解决方案还解决了通过 ViewModel 实例发现所有者窗口的问题。
使用此接口,您可以调用 IDialogService.ShowDialog(ownerViewModel,dialogViewModel)。现在,我从所有者 ViewModel 调用它,这意味着我的 ViewModel 之间有硬引用。如果您使用聚合事件,您可能会从指挥中调用它。
设置最终将在对话框中显示的视图的最小尺寸不会自动设置对话框的最小尺寸。此外,由于对话框中的逻辑树包含 ViewModel,因此您不能只绑定到 WindowContent 元素的属性。 这个问题我的解决方案有答案。
我上面提到的答案还包括将窗口以所有者为中心的代码。
最后,禁用最小化和最大化按钮是 WPF 本身无法做到的。恕我直言,最优雅的解决方案是使用 这个。
I'm answering my own question to help others find all answers I struggled to find in one place. What above seems like a straight forward problem, actually presents multiple problems that I hope to answer sufficiently below.
Here goes.
Your WPF window that will serve as the generic dialog can look something like this:
Following MVVM, the right way to show a dialog is through a mediator. To use a mediator, you typically require some service locator as well. For mediator specific details, look here.
The solution I settled on involved implementing an IDialogService interface that is resolved through a simple static ServiceLocator. This excellent codeproject article has the details on that. Take note of this message in the article forum. This solution also solves the problem of discovering the owner window via the ViewModel instance.
Using this interface, you can call IDialogService.ShowDialog(ownerViewModel, dialogViewModel). For now, I'm calling this from the owner ViewModel, meaning I have hard references between my ViewModels. If you use aggregated events, you will probably call this from a conductor.
Setting the minimum size on the View that will eventually be displayed in the dialog doesn't automatically set the minimum size of the dialog. Also, since the logical tree in the dialog contains the ViewModel, you can't just bind to the WindowContent element's properties. This question has an answer with my solution.
The answer I mention above also includes code that centers the window on the owner.
Finally, disabling the minimize and maximize buttons is something WPF can't natively do. The most elegant solution IMHO is using this.