WPF (MVVM): Closing a view from Viewmodel?

2019-01-21 03:25发布

Anybody come across a clever way of closing a view in a viewmodel using MVVM?

Maybe there is a way of using binding to signal the view (window) to close?

I would really appreciate any input anyone has.

Basically i have a loginView that is bound to a loginViewModel, in the viewmodel (using binding on a command) i test to see if the login is successful and if it is i basically load up a new View (mainview) and attach its datacontext...

but i still have the loginView shown - so i need to signal it to unload..

I was also hoping for a generic solution because i am sure that i am going to need to do this sort of thing in other situations

Any ideas?

11条回答
smile是对你的礼貌
2楼-- · 2019-01-21 03:55

I know this is an old question, but I have a nice way of doing this so I thought I'd share for anyone else stumbling across this. I used to use the dialogcloser attached behavior, but i find the below solution easier where I can use it. The sample below takes an example of a close button on the window for simplicity.

pass the window as the command parameter.

in the button xaml for the view:

CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}"

in the command execute method in the view model:

if (parameter is System.Windows.Window)
{
    (parameter as System.Windows.Window).Close();
}
查看更多
你好瞎i
3楼-- · 2019-01-21 03:55

You can make a command that attaches to the window and when executed closes the window. Then you can bind that command to a property on your view model, and execute the command when you want to close the window.

查看更多
做个烂人
4楼-- · 2019-01-21 03:55

This answer shows another way to do this:

How should the ViewModel close the form?

It uses an attached property to bind the DialogResult window property to a ViewModel property. When setting the value of DialogResult to true or false, the view is closed.

查看更多
疯言疯语
5楼-- · 2019-01-21 03:58

http://adammills.wordpress.com/2009/07/01/window-close-from-xaml/

<Style.Triggers> <DataTrigger Binding="{Binding CloseSignal}" Value="true"> <Setter Property="Behaviours:WindowCloseBehaviour.Close" Value="true" /> </DataTrigger> </Style>

查看更多
看我几分像从前
6楼-- · 2019-01-21 04:00

Edit: See my blog post for a more detailed explanation.

When I need to achieve that, I use a IRequestCloseViewModel interface that I created.

This interface contains only one event: RequestClose. This event is raised by the ViewModel (which inherits from a ViewModelBase class AND implement IRequestCloseViewModel) when it wants to close its associated view.

In my app, all Window inherit from an abstract class ApplicationWindow. This abstract class is notified each time the DataContext changed and in the handler checks if the DataContext support the IRequestCloseViewModel. If this is the case, an event handler is set up to close the Window when the event is fired.

Alternatively, like Kent said, you can use screen controller that handle this mecanism in an external class.

查看更多
登录 后发表回答