问题
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