Skip to content

This example uses the ISupportParentViewModel interface (exposes a parent view model to its child view models).

License

Notifications You must be signed in to change notification settings

DevExpress-Examples/wpf-isupportparentviewmodel

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

39 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

WPF MVVM Framework - Utilize the ISupportParentViewModel Interface

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.

Implementation Details

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));
}

Manual Implementation

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;
}

Files to Review

Documentation

More Examples

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

About

This example uses the ISupportParentViewModel interface (exposes a parent view model to its child view models).

Topics

Resources

License

Stars

Watchers

Forks