Log In
Home
Support
Demos
Documentation
Blogs
Training
Webinars
[Expand]General Information
[Collapse]WinForms Controls
  Prerequisites
 [Expand]What's Installed
 [Collapse]Build an Application
  [Expand]Choose Application UI
   Data Management Controls
  [Expand]Printing and Exporting
  [Collapse]WinForms MVVM
   [Collapse]Concepts
     Conventions and Attributes
     Fluent API Support
     Data Bindings and Notifications
     Commands
     Services
     Behaviors
     Layer Communication. Messenger
     View Management
     ViewModel Management
   [Expand]Design-time Support
   [Expand]Tutorials
  [Expand]Skins
  [Expand]Localization
  [Expand]Right-to-Left Layout
   Redistribution and Deployment
 [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]Document Server
[Expand]Reporting
[Expand]Report Server
[Expand]Dashboard
[Expand]eXpressApp Framework
[Expand]CodeRush
[Expand]CodeRush Classic
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation

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:

How would you rate this topic?​​​​​​​