[Expand]General Information
[Collapse]WinForms Controls
  .NET Core Support
 [Expand]What's Installed
 [Collapse]Build an Application
  [Expand]Choose Application UI
  [Expand]Printing and Exporting
  [Collapse]WinForms MVVM
     Conventions and Attributes
     Data Bindings and Notifications
     Layer Communication. Messenger
     Fluent API Support
     View Management
     ViewModel Management
   [Expand]Design-time Support
  [Expand]Right-to-Left Layout
   Redistribution and Deployment
   How to: Perform Actions On Application Startup
 [Expand]Controls and Libraries
 [Expand]Common Features
  Get More Help
 [Expand]API Reference
[Expand]ASP.NET Controls and MVC Extensions
[Expand]ASP.NET Bootstrap Controls
[Expand]ASP.NET Core Bootstrap Controls
[Expand]WPF Controls
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Expand]Office File API
[Expand]Report and Dashboard Server
[Expand]eXpressApp Framework
[Expand]eXpress Persistent Objects
[Expand]CodeRush Classic
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation
View this topic on docs.devexpress.com (Learn more)

ViewModel Management

This article describes how to retrieve ViewModel instances at runtime. Note that if you utilize the MvvmContext component to build your MVVM-applications at design-time, the component manages ViewModels automatically.

If a ViewModel follows the POCO concept, the MVVM Framework dynamically transforms this ViewModel to a new class containing the necessary infrastructure (to support, for instance, simplified data bindings). The Framework works with dynamically created class instances, which means you cannot initially access these instances at runtime because their types have not been determined.

Use the following options to retrieve a working ViewMode:

The ViewModelSource.Create method

In this approach, you create a ViewModel instance first, then call the SetViewModel method to associate this instance with the specific ViewModel type.

The ViewModelBase class

You can inherit ViewModels from the ViewModelBase class that implements MVVM Framework features. In this case, you can create ViewModel instances directly. Note that you also need to call the SetViewModel method to specify that the Framework should use this instance when the ViewModel is required.

We do not recommend this approach because you lose all the features POCO models provide.

ViewModelCreate events

This approach is designed to work with dependency injection frameworks (such as Ninject). The example below illustrates how this injection works with the Ninject framework.

In this scenario, you need dynamically generated ViewModels and bind them to interfaces (MVVM framework features are lost when binding interfaces to POCO directly). To obtain the required instances, handle the regular (local) or static (global) ViewModelCreate event as follows:

The static ViewModelCreate event is a Weak Event. If it contains a closure to a variable ("kernel" in the example above or the "rootContainer" scope in the Autofac sample below) whose lifetime is shorter than the parent container's lifetime, the VS Garbage Collector can prematurely collect this variable and the event handler may never be invoked.

To fix the potential issue, declare variables as properties/fields at the level of the object from which a subscription is performed.

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