C# Class Catel.MVVM.ViewModelBase

View model base for MVVM implementations. This class is based on the ModelBase, and supports all common interfaces used by WPF.
This view model base does not add any services. The technique specific implementation should take care of that (such as WPF, Silverlight, etc).
Inheritance: ModelBase, IViewModel, INotifyableViewModel, IRelationalViewModel, IUniqueIdentifyable
Show file Open project: Catel/Catel Class Usage Examples

Private Properties

Property Type Description
Cancel Task
CancelAndCloseViewModel Task
CancelViewModel Task
Close Task
CloseViewModel Task
DeserializeFromTombstoning void
INotifyableViewModel void
INotifyableViewModel void
INotifyableViewModel void
IRelationalViewModel void
Initialize Task
InitializeModelInternal void
InitializePropertiesWithAttributes void
InitializeThrottling void
InitializeViewModel Task
InitializeViewModelMetaData Catel.MVVM.ViewModelMetadata
OnChildViewModelClosed void
OnChildViewModelPropertyChanged void
OnClosed void
OnClosing void
OnModelErrorInfoUpdated void
OnModelPropertyChangedInternal void
OnThrottlingTimerTick void
PrepareForTombstoneStateInternal void
RecoverFromTombstoneStateInternal void
Save Task
SaveAndCloseViewModel Task
SaveViewModel Task
SerializeForTombstoning byte[]
UninitializeModelInternal void
UninitializeThrottling void
ValidateViewModelToModelMappings void
ViewModelBase System

Public Methods

Method Description
CancelViewModelAsync ( ) : Task

Cancels the editing of the data.

CloseViewModelAsync ( bool result ) : Task

Closes this instance. Always called after the Cancel of Save method.

InitializeViewModelAsync ( ) : Task

Initializes the view model. Normally the initialization is done in the constructor, but sometimes this must be delayed to a state where the associated UI element (user control, window, ...) is actually loaded. This method is called as soon as the associated UI element is loaded.

It's not recommended to implement the initialization of properties in this method. The initialization of properties should be done in the constructor. This method should be used to start the retrieval of data from a web service or something similar. During unit tests, it is recommended to manually call this method because there is no external container calling this method.

SaveViewModelAsync ( ) : Task

Saves the data.

ToString ( ) : string

Converts the object to a string.

ValidateViewModel ( bool force = false, bool notifyChangedPropertiesOnly = true ) : bool

Validates the specified notify changed properties only.

This method is useful when the view model is initialized before the window, and therefore WPF does not update the errors and warnings.

Protected Methods

Method Description
CancelAsync ( ) : Task

Cancels the editing of the data.

CloseAsync ( ) : Task

Closes this instance. Always called after the Cancel of Save method.

GetAllModels ( ) : object[]

Gets all models that are decorated with the ModelAttribute.

GetChildViewModels ( ) : IEnumerable

Gets the child view models of this view model.

InitializeAsync ( ) : Task

Initializes the view model. Normally the initialization is done in the constructor, but sometimes this must be delayed to a state where the associated UI element (user control, window, ...) is actually loaded. This method is called as soon as the associated UI element is loaded.

It's not recommended to implement the initialization of properties in this method. The initialization of properties should be done in the constructor. This method should be used to start the retrieval of data from a web service or something similar. During unit tests, it is recommended to manually call this method because there is no external container calling this method.

InitializeModel ( string modelProperty, object model ) : void

Called when a model initialized.

InitializeViewModelAttributes ( ) : void

Initializes the view model attributes, such as the ModelAttribute and ViewModelToModelAttribute. This method is automatically invoked by the constructor. Sometimes, dynamic properties are registered after the constructor. Therefore, it is possible to skip the initialization of the attributes and handle this manually.

IsModelRegistered ( string name ) : bool

Determines whether a specific property is registered as a model.

OnClosedAsync ( bool result ) : Task

Called when the view model has just been closed. This method also raises the Closed event.

OnClosingAsync ( ) : Task

Called when the view model is about to be closed. This method also raises the Closing event.

OnModelPropertyChanged ( object sender, PropertyChangedEventArgs e ) : void

Called when a property on one of the registered models has changed.

This method will also raise for properties that are not mapped on the view model.

OnPropertyChanged ( AdvancedPropertyChangedEventArgs e ) : void

Called when a property value has changed.

OnValidated ( IValidationContext validationContext ) : void

Called when the object is validated.

OnValidating ( IValidationContext validationContext ) : void

Called when the object is validating.

OnValidatingBusinessRules ( IValidationContext validationContext ) : void

Called when the object is validating the business rules.

OnValidatingFields ( IValidationContext validationContext ) : void

Called when the object is validating the fields.

OnViewModelCommandExecuted ( IViewModel viewModel, ICatelCommand command, object commandParameter ) : void

Called when a command for a view model type that the current view model is interested in has been executed. This can be accomplished by decorating the view model with the InterestedInAttribute.

OnViewModelEvent ( IViewModel viewModel, ViewModelEvent viewModelEvent, EventArgs e ) : void

Called when an event for a view model type that the current view model is interested in has been raised. This can be accomplished by decorating the view model with the InterestedInAttribute.

OnViewModelPropertyChanged ( IViewModel viewModel, string propertyName ) : void

Called when a property has changed for a view model type that the current view model is interested in. This can be accomplished by decorating the view model with the InterestedInAttribute.

PrepareForTombstoneState ( object>.IDictionary state ) : void

Prepares the state for tombstoning. This method will be called when the TombstoningMode is set to TombstoningMode.Manual.

RaisePropertyChanged ( object sender, AdvancedPropertyChangedEventArgs e ) : void

Raises the ObservableObject.PropertyChanged event. This is the one and only method that actually raises the ObservableObject.PropertyChanged event. All other methods are (and should be) just overloads that eventually call this method.

RecoverFromTombstoneState ( object>.IDictionary state ) : void

Recovers the state from tombstoning. This method will be called when the TombstoningMode is set to TombstoningMode.Manual.

RegisterViewModelServices ( IServiceLocator serviceLocator ) : void

Registers the default view model services.

ResetModel ( string modelProperty, ModelCleanUpMode modelCleanUpMode ) : void

Resets the model by calling uninitializing and initializing the model again. This means that if the model supports IEditableObject, it will be reset.

SaveAsync ( ) : Task

Saves the data.

UninitializeModel ( string modelProperty, object model, ModelCleanUpMode modelCleanUpMode ) : void

Called when a model uninitialized.

UpdateExplicitViewModelToModelMappings ( ) : void

Updates the view model to model mappings that are defined as ViewModelToModelMode.Explicit.

ViewModelBase ( IServiceLocator serviceLocator, bool supportIEditableObject = true, bool ignoreMultipleModelsWarning = false, bool skipViewModelAttributesInitialization = false ) : System

Initializes a new instance of the ViewModelBase class. This constructor allows the injection of a custom IServiceLocator.

ViewModelBase ( bool supportIEditableObject, bool ignoreMultipleModelsWarning = false, bool skipViewModelAttributesInitialization = false ) : System

Initializes a new instance of the ViewModelBase class.

Private Methods

Method Description
Cancel ( ) : Task
CancelAndCloseViewModel ( ) : Task
CancelViewModel ( ) : Task
Close ( ) : Task
CloseViewModel ( bool result ) : Task
DeserializeFromTombstoning ( byte data ) : void

Deserializes the data from a previously stored tombstoning state.

INotifyableViewModel ( IViewModel viewModel, ICatelCommand command, object commandParameter ) : void

Called when a command for a view model type that the current view model is interested in has been executed. This can be accomplished by decorating the view model with the InterestedInAttribute.

This method should only be called by Catel so the ManagedViewModel can invoke it. This method is only used as a pass-through to the actual OnViewModelCommandExecuted method.

INotifyableViewModel ( IViewModel viewModel, ViewModelEvent viewModelEvent, EventArgs e ) : void

Views the model event.

This method should only be called by Catel so the ManagedViewModel can invoke it. This method is only used as a pass-through to the actual OnViewModelEvent method.

INotifyableViewModel ( IViewModel viewModel, string propertyName ) : void

Called when a property has changed for a view model type that the current view model is interested in. This can be accomplished by decorating the view model with the InterestedInAttribute.

This method should only be called by Catel so the ManagedViewModel can invoke it. This method is only used as a pass-through to the actual OnViewModelPropertyChanged method.

IRelationalViewModel ( IViewModel parentViewModel ) : void

Sets the new parent view model of this view model.

Initialize ( ) : Task
InitializeModelInternal ( string modelProperty, object model ) : void

Initializes a model by subscribing to all events.

InitializePropertiesWithAttributes ( ) : void

Initializes the properties with attributes.

InitializeThrottling ( ) : void
InitializeViewModel ( ) : Task
InitializeViewModelMetaData ( Type viewModelType ) : Catel.MVVM.ViewModelMetadata

Initializes the view model meta data. This method only initializes the meta data once per view model type. If a type is already initialized, this method will immediately return.

OnChildViewModelClosed ( object sender, EventArgs e ) : void

Called when the child view model is closed.

OnChildViewModelPropertyChanged ( object sender, PropertyChangedEventArgs e ) : void

Called when a property has changed on the child view model.

OnClosed ( bool result ) : void
OnClosing ( ) : void
OnModelErrorInfoUpdated ( object sender, EventArgs e ) : void

Called when the ModelErrorInfo.Updated event occurs.

OnModelPropertyChangedInternal ( object sender, PropertyChangedEventArgs e ) : void

Handles the PropertyChanged event of a Model.

OnThrottlingTimerTick ( ) : void

Called when the throttling timer ticks.

PrepareForTombstoneStateInternal ( object>.IDictionary state ) : void

Prepares the state for tombstoning.

This method is implemented so the PhoneApplicationPage can call this method.

RecoverFromTombstoneStateInternal ( object>.IDictionary state ) : void

Recovers the state from tombstoning.

This method is implemented so the PhoneApplicationPage can call this method.

Save ( ) : Task
SaveAndCloseViewModel ( ) : Task
SaveViewModel ( ) : Task
SerializeForTombstoning ( ) : byte[]

Serializes the data in the view model for tombstoning.

UninitializeModelInternal ( string modelProperty, object model, ModelCleanUpMode modelCleanUpMode ) : void

Uninitializes a model by unsubscribing from all events.

UninitializeThrottling ( ) : void
ValidateViewModelToModelMappings ( ) : void

Validates the view model to model mappings.

ViewModelBase ( ) : System

Initializes static members of the ViewModelBase class.

Method Details

CancelAsync() protected method

Cancels the editing of the data.
protected CancelAsync ( ) : Task
return Task

CancelViewModelAsync() public method

Cancels the editing of the data.
public CancelViewModelAsync ( ) : Task
return Task

CloseAsync() protected method

Closes this instance. Always called after the Cancel of Save method.
protected CloseAsync ( ) : Task
return Task

CloseViewModelAsync() public method

Closes this instance. Always called after the Cancel of Save method.
public CloseViewModelAsync ( bool result ) : Task
result bool The result to pass to the view. This will, for example, be used as DialogResult.
return Task

GetAllModels() protected method

Gets all models that are decorated with the ModelAttribute.
protected GetAllModels ( ) : object[]
return object[]

GetChildViewModels() protected method

Gets the child view models of this view model.
protected GetChildViewModels ( ) : IEnumerable
return IEnumerable

InitializeAsync() protected method

Initializes the view model. Normally the initialization is done in the constructor, but sometimes this must be delayed to a state where the associated UI element (user control, window, ...) is actually loaded. This method is called as soon as the associated UI element is loaded.
It's not recommended to implement the initialization of properties in this method. The initialization of properties should be done in the constructor. This method should be used to start the retrieval of data from a web service or something similar. During unit tests, it is recommended to manually call this method because there is no external container calling this method.
protected InitializeAsync ( ) : Task
return Task

InitializeModel() protected method

Called when a model initialized.
protected InitializeModel ( string modelProperty, object model ) : void
modelProperty string The name of the model property.
model object The model.
return void

InitializeViewModelAsync() public method

Initializes the view model. Normally the initialization is done in the constructor, but sometimes this must be delayed to a state where the associated UI element (user control, window, ...) is actually loaded. This method is called as soon as the associated UI element is loaded.
It's not recommended to implement the initialization of properties in this method. The initialization of properties should be done in the constructor. This method should be used to start the retrieval of data from a web service or something similar. During unit tests, it is recommended to manually call this method because there is no external container calling this method.
public InitializeViewModelAsync ( ) : Task
return Task

InitializeViewModelAttributes() protected method

Initializes the view model attributes, such as the ModelAttribute and ViewModelToModelAttribute. This method is automatically invoked by the constructor. Sometimes, dynamic properties are registered after the constructor. Therefore, it is possible to skip the initialization of the attributes and handle this manually.
A mapped model is not registered. A mapped model property is not found.
protected InitializeViewModelAttributes ( ) : void
return void

IsModelRegistered() protected method

Determines whether a specific property is registered as a model.
protected IsModelRegistered ( string name ) : bool
name string The name of the registered model.
return bool

OnClosedAsync() protected method

Called when the view model has just been closed. This method also raises the Closed event.
protected OnClosedAsync ( bool result ) : Task
result bool The result to pass to the view. This will, for example, be used as DialogResult.
return Task

OnClosingAsync() protected method

Called when the view model is about to be closed. This method also raises the Closing event.
protected OnClosingAsync ( ) : Task
return Task

OnModelPropertyChanged() protected method

Called when a property on one of the registered models has changed.
This method will also raise for properties that are not mapped on the view model.
protected OnModelPropertyChanged ( object sender, PropertyChangedEventArgs e ) : void
sender object The sender.
e System.ComponentModel.PropertyChangedEventArgs The instance containing the event data.
return void

OnPropertyChanged() protected method

Called when a property value has changed.
protected OnPropertyChanged ( AdvancedPropertyChangedEventArgs e ) : void
e AdvancedPropertyChangedEventArgs The instance containing the event data.
return void

OnValidated() protected method

Called when the object is validated.
protected OnValidated ( IValidationContext validationContext ) : void
validationContext IValidationContext The validation context.
return void

OnValidating() protected method

Called when the object is validating.
protected OnValidating ( IValidationContext validationContext ) : void
validationContext IValidationContext The validation context.
return void

OnValidatingBusinessRules() protected method

Called when the object is validating the business rules.
protected OnValidatingBusinessRules ( IValidationContext validationContext ) : void
validationContext IValidationContext The validation context.
return void

OnValidatingFields() protected method

Called when the object is validating the fields.
protected OnValidatingFields ( IValidationContext validationContext ) : void
validationContext IValidationContext The validation context.
return void

OnViewModelCommandExecuted() protected method

Called when a command for a view model type that the current view model is interested in has been executed. This can be accomplished by decorating the view model with the InterestedInAttribute.
protected OnViewModelCommandExecuted ( IViewModel viewModel, ICatelCommand command, object commandParameter ) : void
viewModel IViewModel The view model.
command ICatelCommand The command that has been executed.
commandParameter object The command parameter used during the execution.
return void

OnViewModelEvent() protected method

Called when an event for a view model type that the current view model is interested in has been raised. This can be accomplished by decorating the view model with the InterestedInAttribute.
protected OnViewModelEvent ( IViewModel viewModel, ViewModelEvent viewModelEvent, EventArgs e ) : void
viewModel IViewModel The view model.
viewModelEvent ViewModelEvent The view model event.
e System.EventArgs The instance containing the event data.
return void

OnViewModelPropertyChanged() protected method

Called when a property has changed for a view model type that the current view model is interested in. This can be accomplished by decorating the view model with the InterestedInAttribute.
protected OnViewModelPropertyChanged ( IViewModel viewModel, string propertyName ) : void
viewModel IViewModel The view model.
propertyName string Name of the property.
return void

PrepareForTombstoneState() protected method

Prepares the state for tombstoning. This method will be called when the TombstoningMode is set to TombstoningMode.Manual.
protected PrepareForTombstoneState ( object>.IDictionary state ) : void
state object>.IDictionary The target state which can be used to store values.
return void

RaisePropertyChanged() protected method

Raises the ObservableObject.PropertyChanged event. This is the one and only method that actually raises the ObservableObject.PropertyChanged event. All other methods are (and should be) just overloads that eventually call this method.
protected RaisePropertyChanged ( object sender, AdvancedPropertyChangedEventArgs e ) : void
sender object The sender.
e AdvancedPropertyChangedEventArgs The instance containing the event data.
return void

RecoverFromTombstoneState() protected method

Recovers the state from tombstoning. This method will be called when the TombstoningMode is set to TombstoningMode.Manual.
protected RecoverFromTombstoneState ( object>.IDictionary state ) : void
state object>.IDictionary The source state to recover values from.
return void

RegisterViewModelServices() protected method

Registers the default view model services.
The is null.
protected RegisterViewModelServices ( IServiceLocator serviceLocator ) : void
serviceLocator IServiceLocator The service locator.
return void

ResetModel() protected method

Resets the model by calling uninitializing and initializing the model again. This means that if the model supports IEditableObject, it will be reset.
The is null.
protected ResetModel ( string modelProperty, ModelCleanUpMode modelCleanUpMode ) : void
modelProperty string The model property.
modelCleanUpMode ModelCleanUpMode The model clean up mode.
return void

SaveAsync() protected method

Saves the data.
protected SaveAsync ( ) : Task
return Task

SaveViewModelAsync() public method

Saves the data.
public SaveViewModelAsync ( ) : Task
return Task

ToString() public method

Converts the object to a string.
public ToString ( ) : string
return string

UninitializeModel() protected method

Called when a model uninitialized.
protected UninitializeModel ( string modelProperty, object model, ModelCleanUpMode modelCleanUpMode ) : void
modelProperty string The name of the model property.
model object The model.
modelCleanUpMode ModelCleanUpMode The model clean up mode.
return void

UpdateExplicitViewModelToModelMappings() protected method

Updates the view model to model mappings that are defined as ViewModelToModelMode.Explicit.
protected UpdateExplicitViewModelToModelMappings ( ) : void
return void

ValidateViewModel() public method

Validates the specified notify changed properties only.
This method is useful when the view model is initialized before the window, and therefore WPF does not update the errors and warnings.
public ValidateViewModel ( bool force = false, bool notifyChangedPropertiesOnly = true ) : bool
force bool If set to true, a validation is forced (even if the object knows it is already validated).
notifyChangedPropertiesOnly bool if set to true only the properties for which the warnings or errors have been changed /// will be updated via ; otherwise all the properties that /// had warnings or errors but not anymore and properties still containing warnings or errors will be updated.
return bool

ViewModelBase() protected method

Initializes a new instance of the ViewModelBase class. This constructor allows the injection of a custom IServiceLocator.
A mapped model is not registered. A mapped model property is not found.
protected ViewModelBase ( IServiceLocator serviceLocator, bool supportIEditableObject = true, bool ignoreMultipleModelsWarning = false, bool skipViewModelAttributesInitialization = false ) : System
serviceLocator IServiceLocator The service locator to inject. If null, the will be used.
supportIEditableObject bool if set to true, the view model will natively support models that /// implement the interface.
ignoreMultipleModelsWarning bool if set to true, the warning when using multiple models is ignored.
skipViewModelAttributesInitialization bool if set to true, the initialization will be skipped and must be done manually via .
return System

ViewModelBase() protected method

Initializes a new instance of the ViewModelBase class.
A mapped model is not registered. A mapped model property is not found.
protected ViewModelBase ( bool supportIEditableObject, bool ignoreMultipleModelsWarning = false, bool skipViewModelAttributesInitialization = false ) : System
supportIEditableObject bool if set to true, the view model will natively support models that /// implement the interface.
ignoreMultipleModelsWarning bool if set to true, the warning when using multiple models is ignored.
skipViewModelAttributesInitialization bool /// if set to true, the initialization will be skipped and must be done manually via . ///
return System