What actually happens when ApplicationCommands.Close is executed

杀马特。学长 韩版系。学妹 提交于 2021-02-07 19:18:02

问题


I guess the question in the title is clear enough.What happens when i call

ApplicationCommands.Close.Execute(null,null)

from my viewmodel class.

I have a modeldialog which shows a usercontrol.I have a commandbinding to a button in the usercontrol which is executed in the viewmodel.I want to close the dialog after the command is executed.It can be done by using the above command to the end of the executed event.

But i am wondering if this is the right approach.Will this cause any undesired effect ?


回答1:


Unfortunately this won't work.

ApplicationCommands.Close is a RoutedUiEvent which requires a Commandtarget (any IInputelement). It uses this command target to raise a routedevent.

You can then handle this routedevent via using a commandbinding.

From the ViewModel you could bind ApplicationCommands.Close to Close Button and then handle the routed close event in your window.

However you can't call ApplicationCommands.Close.Execute(null,null) in your ViewModel after another command is executed as you have no CommandTarget (the second null)

There are other threads like this one How should the ViewModel close the form? which detail various methods to acheive this.

http://adammills.wordpress.com/2011/02/16/mvvm-uses-for-applicationcommands-close/



来源:https://stackoverflow.com/questions/4582540/what-actually-happens-when-applicationcommands-close-is-executed

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!