Handling the close button "X" in WPF with MVVM

I am building a basic database application in WPF and I started using the MVVM pattern.

I have a dialog that asks the user to select an item from ListBoxand click OK. After that, I take the element that the user clicked on the object in the view model and passed it to another dialog box. But if the user clicks “Cancel”, I set this value to null, and the action is canceled: I do not open the next dialog box and do not 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 Close method
        RequestClose();
    }

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

        base.RequestClose();
    }
}

, , "X" . RequestClose , null, .

Closing , , , .

"" ?

.

+3
3

, EventToCommand Window ExecuteCancel .

public void ExecuteCancel() 
{ 
    _selected = false; 

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

, ? "", ExecuteCancel...

+1

. xaml.

( )

0

Source: https://habr.com/ru/post/1752936/


All Articles