WPF MVVM:如何关闭窗口 [英] WPF MVVM: How to close a window

查看:1709
本文介绍了WPF MVVM:如何关闭窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按钮,关闭我的窗口,当它被点击:

I have a button that closes my window when it's clicked:

<Button x:Name="buttonOk"  IsCancel="True">Ok</Button>

这很好,直到我添加一个命令按钮即

That's fine until I add a command to the button i.e.

<Button x:Name="buttonOk" Command="{Binding SaveCommand}" IsCancel="True">Ok</Button>

现在不会关闭presumably因为我处理的命令。我可以把一个事件处理程序,并调用this.Close(),即解决这个问题

Now it doesn't close presumably because I am handling the Command. I can fix this by putting an event handler in and calling this.Close() i.e.

<Button x:Name="buttonOk" Click="closeWindow" Command="{Binding SaveCommand}" IsCancel="True">Ok</Button>

但现在我有code在我的code的后面即法SaveCommand。我使用MVVM模式和SaveCommand是唯一的code在我的code后面。

but now I have code in my code behind i.e. the method "SaveCommand". I am using the MVVM pattern and SaveCommand is the only code in my code behind.

我怎样才能做到这一点不同,因此尽量不要使用code后面?

How can I do this differently so as not to use code behind?

推荐答案

所以你需要做相当多的基础性工作或使用MVVM框架,比如Cinch 。如果你想投资的时候自己做这里的束带如何链接做的。

Unfortunately displaying windows is a real pain in MVVM so you need to do quite a bit of infrastructure work or use a MVVM framework like Cinch. If you want to invest the time to do it yourself here's a link of how Cinch does it.

其良好的,你想保持的任何逻辑出了查看,但世界的真没底,如果你这样做。在这种情况下它听起来并不像它会造成太多的问题。

Its good that you're trying to keep any logic out of the View but its really not the end of the world if you do. In this instance it doesn't sound like it would cause too many problems.

这篇关于WPF MVVM:如何关闭窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆