user153498
user153498

Reputation:

Handling the "X" close button in WPF under MVVM

I'm creating a basic database application in WPF, and I have started using the MVVM pattern.

I have a dialog that asks the user to select an item from a ListBox and click "OK." After that, I take the item the user clicked from a property in the view model and pass it into another dialog. But if the user clicks "Cancel," I set that value to null, and the action is canceled: I don't open the next dialog and return to the main screen. Example:

public class SelectEquipmentViewModel : WorkspaceViewModel
{
    private bool _selected;

    public Equipment SelectedEquipment
    {
        // Item selected by the user
    }

    // Action for "SelectCommand," which is attached to
    // the "Select" button in the view
    public void ExecuteSelect()
    {
        _selected = true;

        // Fires a RequestClose event in WorkspaceViewModel,
        // which is attached to the view's Close method
        RequestClose();
    }

    public override void RequestClose()
    {
        if (!_selected)
        {
            // The user clicked "Cancel"
            SelectedEquipment = null;
        }

        base.RequestClose();
    }
}

This has been working great, but the problem comes if the user clicks the red "X" close button in the window's control box. The RequestClose method never gets invoked, and the selected item isn't set to null, which is bad.

I've considered attaching the view model to the Closing event of the view, but I feel this could get messy if I start creating handlers for all these events.

What would be the "preferred" way of handling this situation?

Thanks.

Upvotes: 4

Views: 7997

Answers (3)

sergtk
sergtk

Reputation: 10974

Approach without additional dependencies is described in article Handling a Window's Closed and Closing events in the View-Model and code with example provided. This does not add code behind xaml.

(Thanks to Reed Copsey's link)

Upvotes: 0

Brent
Brent

Reputation: 1773

Behaviors are what you want to use to execute a command when the user presses the "X" button on window using MVVM. Check out Reed Copsey's blog

You can download a sample application here...

I use this method all the time to allow the ViewModel manage the life of the view.

Upvotes: 0

Chris Koenig
Chris Koenig

Reputation: 2748

I think that using the EventToCommand behavior to wire up the Window object's Closing event to a new ExecuteCancel command is pretty clean.

public void ExecuteCancel() 
{ 
    _selected = false; 

    // Fires a RequestClose event in WorkspaceViewModel, 
    // which is attached to the view's Close method 
    RequestClose(); 
}

Where do you think this will get messy? If you add a Cancel button, it could use the same ExecuteCancel bits...

Upvotes: 1

Related Questions