Caliburn Micro cancel window close from ViewModel

Kristoffer Lindvall picture Kristoffer Lindvall · Jan 9, 2012 · Viewed 8.3k times · Source

When the user clicks the close button of a Window, is it possible to cancel the close from the ViewModel or do I have to resort to code behind?

From what I can tell, CanClose or TryClose doesn't do the trick.

Answer

ChrisWay picture ChrisWay · Jan 9, 2012

You may have already tried this but I've just created a quick test, deriving a view model from Screen and overriding CanClose.

public class ShellViewModel : Screen
{
    public override void CanClose(Action<bool> callback)
    {
        //if(some logic...)
        callback(false); // will cancel close
    }
}