Auto ViewModelLocator In MVVM Pattern

Auto ViewModelLocator

I will present a little trick inside of the ViewModels instantiation in MVVM pattern.

Sometimes, we work in WPF small solutions, which does not usually need ViewModelLocator class as ViewModels class generator, because we don’t need to save ViewModels reference. We will not worry about feeding  ViewModelLocator class and we will go to deploy our ViewModels classes immediately. This fits perfectly with the unit tests and binding engine.

We will get all this with a class with two WPF attached properties given below.

We will have two cases, which are given below.

  • The View name and ViewModel name will have equivalent names.


    • SameNameClass[View].xaml
    • SameNameClass[ViewModel].cs

  • View name and ViewModel name don’t have equivalent names.


    • DifenteNameView.xaml
    • DiferentNameViewModel.cs

Equivalents Names (View and ViewModel)

In this case, proceed, as shown below.

  1. <Window x:Class="AutoMVVMLocator.Example1View"  
  2.         xmlns=""  
  3.         xmlns:x=""  
  4.         xmlns:d=""  
  5.         xmlns:mc=""  
  6.         xmlns:local="clr-namespace:AutoMVVMLocator"  
  7.         local:MLMVVM.IsAutomaticLocator="True"  
  8.         mc:Ignorable="d"  
  9.         Title="Example1View" Height="300" Width="300">  
  11. local:MLMVVM.IsAutomaticLocator = “True”  

In this example, the view Example1View instances an Example1ViewModel object as DataContext automatically.

Not Equivalents Names (View and ViewModel)

In this case, we need to specify another property with the name of ViewModel class.

  1. <Window x:Class="AutoMVVMLocator.Example2Window"  
  2.         xmlns=""  
  3.         xmlns:x=""  
  4.         xmlns:d=""  
  5.         xmlns:mc=""  
  6.         xmlns:local="clr-namespace:AutoMVVMLocator"  
  7.         local:MLMVVM.ViewModelClassName="Example2ViewModel"  
  8.         local:MLMVVM.IsAutomaticLocator="True"  
  9.         mc:Ignorable="d"  
  10.         Title="Example2Window" Height="300" Width="396.546">  
  12. local:MLMVVM.ViewModelClassName = “Example2ViewModel”  
  13. local:MLMVVM.IsAutomaticLocator = “True”  


The property ViewModelClassName has to be in first place.

In this example, the view Example2Window instances an Example2ViewModel object as DataContext automatically.

MLMVVM class

It is a very simple class. It has two AttachProperties and two private methods.

The attachproperties are the previously configured properties.

  • Activate the automatic instance ViewModels classes.
  • Show the ViewModel class name to the instance. If your ViewModelClass name is equivalent with the View name, its property is not necessary.
  1. public static bool GetIsAutomaticLocator(DependencyObject obj) {  
  2.     return (bool) obj.GetValue(IsAutomaticLocatorProperty);  
  3. }  
  4. public static void SetIsAutomaticLocator(DependencyObject obj, bool value) {  
  5.     obj.SetValue(IsAutomaticLocatorProperty, value);  
  6. }  
  7. public static readonly DependencyProperty IsAutomaticLocatorProperty = DependencyProperty.RegisterAttached("IsAutomaticLocator"typeof(bool), typeof(MLMVVM), new PropertyMetadata(false, IsAutomaticLocatorChanged));  
  8. private static void IsAutomaticLocatorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {  
  9.     var callOwner = d as FrameworkElement;  
  10.     var className = GetViewModelClassName(d);  
  11.     var userControl = GetInstanceOf(callOwner.GetType(), className);  
  12.     callOwner.DataContext = userControl;  
  13. }  
  14. public static string GetViewModelClassName(DependencyObject obj) {  
  15.     return (string) obj.GetValue(ViewModelClassNameProperty);  
  16. }  
  17. public static void SetViewModelClassName(DependencyObject obj, string value) {  
  18.     obj.SetValue(ViewModelClassNameProperty, value);  
  19. }  
  20. public static readonly DependencyProperty ViewModelClassNameProperty = DependencyProperty.RegisterAttached("ViewModelClassName"typeof(string), typeof(MLMVVM), new PropertyMetadata(null));  

The two private methods contains a dynamic instance engine.

  1. private static object GetInstanceOf(Type dependencyPropertyType, string className)  
  2. {  
  3.     var assembly = dependencyPropertyType.Assembly;   
  5.     var assemblyTypes = assembly.GetTypes();  
  7.     var classNameDef = GetClassName(dependencyPropertyType, className);  
  9.     var userControlType = assemblyTypes.FirstOrDefault(a => a.Name.Contains(classNameDef));  
  11.     if (userControlType == nullthrow new ArgumentException($"Not exist a type {classNameDef} in the asembly {assembly.FullName}");  
  13.     var resultado = Activator.CreateInstance(userControlType);  
  15.     return resultado;  
  16. }  
  18. private static string GetClassName(Type dependencyPropertyType, string className)  
  19. {  
  20.     if (string.IsNullOrWhiteSpace(className)) return $"{dependencyPropertyType.Name}Model";  
  22.     return className;  
  23. }  


If you work in a great project or you need recover the references of the instances ViewModels, you must use a classical ViewModelLocator class.

Test Project

In the Test Project, we have the code and example for all the cases.