Skip to main content

View Injection Service Concept

  • 3 minutes to read

The Concept of the View Injection Service

The main benefit of the ViewInjectionService is that it provides a common mechanism to control ViewModels and their interaction.

To start using this service, add it to the control’s dxmvvm:Interaction.Behaviors.

<UserControl x:Class="DXSample.View.MainView"
    ...
    xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm"
    xmlns:common="clr-namespace:DXSample.Common">
    <Grid>
        <TabControl>
            <dxmvvm:Interaction.Behaviors>
                <dxmvvm:ViewInjectionService/>
            </dxmvvm:Interaction.Behaviors>
        </TabControl>
    </Grid>
</UserControl>

Use any of the following approaches to access the defined ViewInjectionservice from your ViewModel.

To integrate a ViewModel (with its View) to the TabControl, use the ViewInjectionServiceExtensions.Inject method. This method has three overloads with different parameters.

public static class ViewInjectionServiceExtensions {
    public static void Inject(this IViewInjectionService service, object key, object viewModel);
    public static void Inject(this IViewInjectionService service, object key, object viewModel, string viewName);
    public static void Inject(this IViewInjectionService service, object key, object viewModel, Type viewType);
    ...
}
  • The first method overload injects the ViewModel passed to a control using viewModel parameter. The View is defined in the control’s ItemTemplate or ContentTemplate.
  • The other two method overloads use the ViewLocator to create a View by using viewName or viewType arguments and pass the specified ViewModel to the created View.

Tip

A more advanced way to integrate ViewModels and Views into a control is to use ViewInjectionManager. This concept is described in the View Injection Manager Concept topic.

Below is a list of other general ViewInjectionService properties and members.

public sealed class ViewInjectionService : ServiceBase, IViewInjectionService {
    public string RegionName { ... }
    public IEnumerable<object> ViewModels { ... }
    public object SelectedViewModel { ... }
    public ICommand SelectedViewModelChangedCommand { ... }
    public ICommand ViewModelClosingCommand { ... }
    ...
}
See Also