wpf-113942-mvvm-framework-services-predefined-set-viewinjectionservice-view-injection-service-concept.md
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);
...
}
Public NotInheritable Class ViewInjectionServiceExtensions
_
Public Shared Sub Inject(service As IViewInjectionService, key As Object, viewModel As Object)
End Sub
_
Public Shared Sub Inject(service As IViewInjectionService, key As Object, viewModel As Object, viewName As String)
End Sub
_
Public Shared Sub Inject(service As IViewInjectionService, key As Object, viewModel As Object, viewType As Type)
End Sub
...
End Class
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 { ... }
...
}
Public NotInheritable Class ViewInjectionService
Inherits ServiceBase
Inherits ViewInjectionService
Public Property RegionName() As String
...
End Property
Public Property ViewModels() As IEnumerable(Of Object)
...
End Property
Public Property SelectedViewModel() As Object
...
End Property
Public Property SelectedViewModelChangedCommand() As ICommand
...
End Property
Public Property ViewModelClosingCommand() As ICommand
...
End Property
End Class
See Also