[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
 [Expand]What's Installed
 [Expand].NET Core 3 Support
 [Expand]Common Concepts
 [Collapse]MVVM Framework
  [Expand]View Models
    Getting Started
    Services in ViewModelBase descendants
    Services in POCO objects
    Services in custom ViewModels
    View creation mechanisms
   [Expand]Predefined Set
    How to create a Custom Service
   Data Annotation Attributes
   Weak Event
 [Expand]Controls and Libraries
 [Expand]Scaffolding Wizard
  Redistribution and Deployment
  Get More Help
 [Expand]API Reference
[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)

View creation mechanisms

Some services provide the capability to display child Views (like IDialogService, IDocumentManagerService, etc.). Usually, these services are derived from the ViewServiceBase class. This class provides three properties that can be set in XAML: ViewTemplate, ViewTemplateSelector, and ViewLocator. All services derived from the ViewServiceBase class support these properties and provide three approaches of creating child Views.

Each creation mechanism is used in a specific application architecture. You can find more information about architecture types of MVVM applications in the following documentation topic: Interaction of ViewModels.

In this article, you will review all the approaches on the DialogService example. This service allows you to display child Views in a modal window. Please review the following documentation article about the DialogService: DialogService.

Expanded ViewTemplate and ViewTemplateSelector, tightly-coupled View Models

This approach is usually used in the tightly-coupled View Models architecture, in which View Models may have direct links to other View Models and create them. The DialogService documentation topic contains a tutorial and an example that demonstrate the ViewTemplate and ViewTemplateSelector view creation approach. In that example, a child View is defined in XAML.

The ChildViewModel is created from the MainViewModel and passed as a parameter to the DialogService.

In this case, the ChildView is created based on the defined DialogService.ViewTemplate, and the passed ChildViewModel is assigned to the DataContext of the created ChildView.

Alternatively, a View can be dynamically generated from the passed ViewModel. The DialogService.ViewTemplateSelector property supports this approach. The code below demonstrates it.

This approach is useful if you want to show different Views with different View Models using only one service. Alternatively, you can place several services with different names. Please review the following articles for more details.

Services in ViewModelBase descendants

Services in POCO objects

Expanded ViewLocator, tightly-coupled View Models

The ViewLocator provides a simple composition mechanism that for creating child Views by names.

The preceding code implicitly calls the default ViewLocator to create the ChildView based on the passed document type parameter. Since, the ChildViewModel is created from the MainViewModel, you can set any parameter of the created ChildViewModel, and then pass it to the specified ChildView through the DialogService. This approach also implies a tightly-coupled View Model architecture.

The ViewServiceBase class provides the ViewLocator property. By default, this property is null, which means that the service uses the default ViewLocator (ViewLocator.Default). If the default ViewLocator implementation does not meet your requirements, you can change the default ViewLocator (by setting the ViewLocator.Default property) or define a specific ViewLocator at the service level (by setting the ViewServiceBase.ViewLocator property).

Expanded ViewLocator, loosely-coupled View Models

If you use the loosely-coupled architecture, you can create child Views, without passing View Models to the child Views.

This approach involves defining the ChildViewModel in the ChildView XAML.

In the case above, the ChildView is created based on the document type parameter. The ChildViewModel is not passed through the DialogService, because it is already defined in the ChildView XAML.

To implement interaction between View Models, you can pass a parameter to the ChildViewModel through the ISupportParameter interface. See the Passing Data Between ViewModels (ISupportParameter) article for more details.

The last parameter in the DialogService.ShowDialog invocation is a parent view model. With this parameter, you can pass the main View Model to the child one to allow the child View Model to access services defined in the main View Model. See the ViewModel relationships (ISupportParentViewModel) article for more details.

Expanded Summary

Services derived from the ViewServiceBase class support an interface in which there is a particular method for creating child Views. This method has the following parameters.

  • DocumentType (String) - is used to create a child View through the ViewLocator.
  • ViewModel (Object) - a View Model that is passed to the DataContext of the created child View.
  • Parameter (Object) - an object that is passed to the parameter of the child View Model (the child View Model should support the ISupportParameter interface).
  • ParentViewModel (Object) - an object that is set to the ISupportParentViewModel.ParentViewModel property of the child View Model.

The method that takes all the parameters is usually hidden. Instead, there are several extension methods.

  • Method1(object ViewModel) - this method is used when a child View is defined though the ViewServiceBase.ViewTemplate or ViewServiceBase.ViewTemplateSelector property. The child View should not contain a View Model, because it is passed through the service. (ViewTemplate and ViewTemplateSelector, tightly-coupled View Models).
  • Method2(string DocumentType, object ViewModel) - this method uses the ViewLocator to create a child View and passes the specified View Model to the created child View. (ViewLocator, tightly-coupled View Models).
  • Method3(string DocumentType, object Parameter, object ParentViewModel) - the method creates a child View through the ViewLocator. The child View already contains a View Model. The Parameter and ParentViewModel are passed to the child View Model. (ViewLocator, loosely-coupled View Models).

For instance, the main ShowDialog method in the IDialogService interface is hidden.

And there are extension methods for this interface.

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