处理对话框在WPF中与MVVM [英] Handling Dialogs in WPF with MVVM
问题描述
在为WPF MVVM模式,处理的对话框是更复杂的操作中的一个。由于您的视图模型不知道有关视图什么,对话沟通可以是有趣的。我可以公开一个ICommand当视图调用它,一个对话框会出现。
In the MVVM pattern for WPF, handling dialogs is one of the more complex operations. As your view model does not know anything about the view, dialog communication can be interesting. I can expose an ICommand that when the view invokes it, a dialog can appear.
有谁知道一个好的方法来处理从对话框的结果吗?我只是讲Windows对话框如MessageBox的。
Does anyone know of a good way to handle results from dialogs? I am speaking about windows dialogs such as MessageBox.
一,我们这样做是对的视图模型,视图将订阅当被要求一个对话框,以一个事件。该方式
One of the ways we did this was have an event on the viewmodel that the view would subscribe to when a dialog was required.
public event EventHandler<MyDeleteArgs> RequiresDeleteDialog;
这是正常的,但它意味着该视图需要code这是一件好事,我想远离。
This is OK, but it means that the view requires code which is something I would like to stay away from.
推荐答案
我建议放弃了1990年的模态对话框,而是实施控制作为覆盖(帆布+绝对定位),能见度绑在布尔早在VM。走近一个Ajax类型的控制。
I suggest forgoing the 1990's modal dialogs and instead implementing a control as an overlay (canvas+absolute positioning) with visibility tied to a boolean back in the VM. Closer to an ajax type control.
这是非常有用的:
<BooleanToVisibilityConverter x:Key="booltoVis" />
在
<my:ErrorControl Visibility="{Binding Path=ThereWasAnError, Mode=TwoWay, Converter={StaticResource booltoVis}, UpdateSourceTrigger=PropertyChanged}"/>
下面就是我有一个实现为用户控件。点击X关闭在用户控件的$ C $后面℃的线code控制。 (因为我有我的一个.exe的意见和的ViewModels在DLL中,我不觉得code表示操纵UI不好的。)
Here's how I have one implemented as a user control. Clicking on the 'x' closes the control in a line of code in the usercontrol's code behind. (Since I have my Views in an .exe and ViewModels in a dll, I don't feel bad about code that manipulates UI.)
这篇关于处理对话框在WPF中与MVVM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!