[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)

ViewModel relationships (ISupportParentViewModel)

View Models inherited from ViewModelBase can be related to each other with a parent-child relationship. This is achieved with the ISupportParentViewModel interface, implemented in the ViewModelBase class. If you use the POCO mechanism, the ISupportParentViewModel interface is automatically implemented when you create a POCO object with the ViewModelSource class.

The ISupportParentViewModel interface is shown below.

When you create a Detail View Model from another View Model, you can pass the main View Model to the detail using the ISupportParentViewModel.ParentViewModel property. This allows the detail View Model to access Services that are registered for the main View Model.

The ParentViewModel parameter can be set in XAML with the ViewModelExtensions.ParentViewModel attached property. The scheme demonstrating how the Main ViewModel is propagated to a Detail ViewModel is shown in the following diagram:

Here, the Main View contains an instance of a Detail View. The DataContexts of each are the Main ViewModel and Detail ViewModel respectively. The task is to pass the Main ViewModel to the Detail ViewModel. To accomplish this, assign the Main ViewModel to the ViewModelExtensions.ParentViewModel attached property in the Detail View instance.

The ViewModelExtensions class, which exposes the ParentViewModel attached property in the Detail View instance, performs all necessary actions to pass data to the Detail ViewModel. When the ParentViewModel attached property is set on the Detail View, the ViewModelExtensions class identifies whether the View's DataContext is a ViewModel (a ViewModelBase object; or to be more precise, whether the DataContext is an object that implements the ISupportParentViewModel interface). And if so, the new value is passed to the Detail View's ViewModel (it is assigned to the ViewModel's ISupportParentViewModel.ParentViewModel private property). As a result of changing the ParentViewModel private property, the Detail ViewModel calls its OnParentViewModelChanged protected method, which you can manually override to obtain and use the Main ViewModel.

The following documentation topic contains an example of how to use services when View Models are related to each other with the parent-child relationship: DXMessageBoxService.

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