This example uses the ISupportParentViewModel
interface (exposes a parent view model to its child view models). This interface is implemented automatically across all ViewModelBase
descendants.
Use the ViewModelExtensions.ParentViewModel
attached property to associate a child view model with a parent view model. In XAML, specify the ParentViewModel
property for the child view as follows so it can access services and data from the parent view model:
<local:ChildView
dxmvvm:ViewModelExtensions.ParentViewModel="{Binding DataContext, ElementName=LayoutRoot}"/>
Note: The
ViewModelExtensions.ParentViewModel
attached property is set after the child view is initialized because the property is not available in a child view constructor.
The ISupportParentViewModel
interface allows a child view model to access MVVM services defined at the parent level:
IMessageBoxService MessageBoxService {
get { return GetService<IMessageBoxService>(ServiceSearchMode.PreferParents); }
}
Override the OnParentViewModelChanged
method in a ViewModelBase
descendant only if your child view model implementation includes custom parent view model changes.
protected override void OnParentViewModelChanged(object parentViewModel) {
RaisePropertyChanged(nameof(ISupportParentViewModel.ParentViewModel));
}
If your view model does not inherit from ViewModelBase
, you can implement the ISupportParentViewModel
interface manually.
The following example implements the ISupportParentViewModel
interface along with ISupportServices
and INotifyPropertyChanged
:
public class CustomChildViewModel : ISupportParentViewModel, ISupportServices, INotifyPropertyChanged {
public object ParentViewModel {
get => _parentViewModel;
set {
if (Equals(value, _parentViewModel)) return;
_parentViewModel = value;
OnPropertyChanged();
}
}
IMessageBoxService MessageBoxService {
get => GetService<IMessageBoxService>(ServiceSearchMode.PreferParents);
}
protected virtual T GetService<T>(ServiceSearchMode searchMode) where T : class {
return this.ServiceContainer.GetService<T>(searchMode);
}
protected IServiceContainer ServiceContainer {
get {
if (serviceContainer == null)
serviceContainer = new ServiceContainer(this);
return serviceContainer;
}
}
IServiceContainer ISupportServices.ServiceContainer => ServiceContainer;
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([System.Runtime.CompilerServices.CallerMemberName] string propertyName = null) {
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private object _parentViewModel;
private IServiceContainer serviceContainer;
}
- MainView.xaml (CS) / MainView.xaml (VB)
- ChildViewModel.cs / ChildViewModel.vb
- CustomChildViewModel.cs / CustomChildViewModel.vb
- WPF MVVM Framework - Use View Models Generated at Compile Time
- WPF Dock Layout Manager - Bind the View Model Collection with LayoutAdapters
- WPF Dock Layout Manager - Bind the View Model Collection with IMVVMDockingProperties
- WPF Dock Layout Manager - Populate a DockLayoutManager LayoutGroup with the ViewModels Collection
- Add the Loading Decorator to the Application with the MVVM Structure
- WPF Hamburger Menu Control - Navigate Between the MVVM Views
- Reporting for WPF - How to Use ViewModel Data as Report Parameters in a WPF MVVM Application
- Reporting for WPF - How to Use the DocumentPreviewControl in a WPF MVVM Application to Preview a Report
(you will be redirected to DevExpress.com to submit your response)