[Expand]General Information
[Expand]WinForms Controls
[Expand]ASP.NET Controls and MVC Extensions
[Expand]ASP.NET Bootstrap Controls
[Expand]ASP.NET Core Bootstrap Controls
[Collapse]WPF Controls
  Prerequisites
 [Expand]What's Installed
 [Expand].NET Core 3 Support
 [Expand]Common Concepts
 [Collapse]MVVM Framework
  [Collapse]View Models
    BindableBase
    ViewModelBase
    POCO ViewModels
    Interaction of ViewModels
    Passing Data Between ViewModels (ISupportParameter)
    ViewModel relationships (ISupportParentViewModel)
  [Expand]Commands
  [Expand]Behaviors
  [Expand]Services
  [Expand]DXBinding
  [Expand]MIF
   Converters
   ViewLocator
   Messenger
   Data Annotation Attributes
   LayoutTreeHelper
   Weak Event
 [Expand]Controls and Libraries
 [Expand]Scaffolding Wizard
 [Expand]Localization
  Redistribution and Deployment
  Get More Help
 [Expand]API Reference
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Expand]Office File API
[Expand]Reporting
[Expand]Report and Dashboard Server
[Expand]Dashboard
[Expand]eXpressApp Framework
[Expand]eXpress Persistent Objects
[Expand]CodeRush
[Expand]CodeRush Classic
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation
View this topic on docs.devexpress.com (Learn more)

ViewModelBase

The ViewModelBase class is a BindableBase descendant. It inherits features of the BindableBase class (such as the GetProperty, SetProperty, and RaisePropertyChanged/RaisePropertiesChanged methods) and offers the following additional capabilities.

Expanded Initialize properties separately for runtime and design-time modes

A View Model may contain a property that requires access to a database. While you are working in the Visual Studio designer, the View Model is not allowed to connect to the database. This leads to an error in the designer.

In such cases, the ViewModelBase class provides the OnInitializeInDesignMode and OnInitializeInRuntime protected virtual methods, which you can override to initialize properties for runtime and design time modes separately.

Expanded Access Services registered within a View

The ViewModelBase class implements the ISupportServices interface that maintains the Services mechanism. The ViewModelBase.GetService method, which employs ISupportServices interface, allows you to access services registered in a View.

The following topic contains more information on how to use services in View Models inherited from the ViewModelBase class: Services in ViewModelBase descendants.

Expanded Use View Model parent-child relationships

View Models inherited from the ViewModelBase can be related to each other with a parent-child relationship. This is achieved with the ISupportParentViewModel interface that is implemented in the ViewModelBase class. In this case, child View Models may access Services registered in the main View Model. The following topic contains more information on how you can set the parent-child relationship and what benefits you can get using this mechanism: ViewModel relationships (ISupportParentViewModel).

Expanded Pass data between View Models

The ViewModelBase class implements the ISupportParameter interface, which can be used for passing initial data to View Models. The following documentation topic describes how this mechanism operates: Passing Data Between ViewModels (ISupportParameter).

Expanded Create Commands without command properties

The ViewModelBase class implements the ICustomTypeDescriptor interface to provide the capability to automatically create command properties based on methods (with the Command attribute) at runtime.

The traditional approach of creating commands is to declare command properties as follows.

Deriving from the ViewModelBase allows you to utilize a shorter definition. Set the Command attribute for the method you wish to use as a command.

When the Command attribute is applied to a method, the corresponding command will have the name: [MethodName]Command" where [MethodName] is the name of the target method. For the example above, the command name will be "SaveCommand".

The Command attribute can be applied to a parameter-less method or to a method with one parameter. You can also customize the command by setting Command attribute properties.

The following topic contains more information about commands: Commands.

Expanded Example

This example demonstrates how to use the ViewModelBase class for inheriting view models.

Is this topic helpful?​​​​​​​