Table of Contents

Class ClosableViewModelBase

Namespace
EasySoftware.MvvmMini
Assembly
EasySoftware.MvvmMini.dll

Base class for view models with a lifecycle that ends when they are closed, such as a window, a dialog or a document tab that the user opens, works with and then closes.

public abstract class ClosableViewModelBase : ViewModelBase, IClosableViewModel, IViewModel, IModel, INotifyPropertyChanged, INotifyDataErrorInfo, IErrorContainer
Inheritance
ClosableViewModelBase
Implements
Derived
Inherited Members

Remarks

Closing starts from CloseCommand or, when the view is a window, from the user closing the window. In both cases OnClosing(CancelEventArgs) is called first. Override it to ask, for example, "Do you want to save changes?" and cancel closing when the user chooses Cancel.

When closing goes ahead, the view is closed (if it is a window), State becomes Closed and Closed is raised.

Constructors

ClosableViewModelBase(IViewAdapter)

Initializes a new instance in the Open state.

protected ClosableViewModelBase(IViewAdapter viewAdapter)

Parameters

viewAdapter IViewAdapter

The adapter of the view that belongs to this view model.

Properties

CloseCommand

Gets the command that asks the view model to close. Bind it to a Close button, a tab's close icon or a menu item. Closing can be cancelled in OnClosing(CancelEventArgs), and the command is available only while CanClose() returns true.

public ICommand CloseCommand { get; }

Property Value

ICommand

State

Gets or sets the lifecycle state of the view model. Setting it to Closed raises Closed.

public ViewModelState State { get; set; }

Property Value

ViewModelState

Methods

CanClose()

Determines whether CloseCommand can execute, for example to disable closing while a save is in progress. Returns true by default. Unlike OnClosing(CancelEventArgs), it does not ask the user anything.

protected virtual bool CanClose()

Returns

bool

true if the view model can be closed; otherwise false.

OnClosing(CancelEventArgs)

Called before the view model is closed, either by CloseCommand or by the user closing the window. Override it to decide whether closing may go ahead, for example to ask the user whether to save unsaved changes, and set Cancel to true to stay open. Does nothing by default.

public virtual Task OnClosing(CancelEventArgs e)

Parameters

e CancelEventArgs

The event data. Set Cancel to true to cancel closing.

Returns

Task

A task that completes when the view model has decided whether to close.

Events

Closed

Occurs after the view model has been closed. The owner of the view model, for example the shell that hosts it as a document, can handle it to remove and release the view model.

public event EventHandler Closed

Event Type

EventHandler