I wrote a really simple ViewModel Locator :
public static readonly DependencyProperty LocateForProperty =
DependencyProperty.RegisterAttached("LocateFor",
typeof (Object),
typeof (ViewModelLocator),
new PropertyMetadata(null, OnLocateForChanged));
public static object GetLocateFor(DependencyObject dependencyObject)
{
return dependencyObject.GetValue(LocateForProperty);
}
public static void SetLocateFor(DependencyObject dependencyObject, Object value)
{
dependencyObject.SetValue(LocateForProperty, value);
}
private static void OnLocateForChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs eventArgs)
{
if (dependencyObject != null)
{
var viewModelName = dependencyObject.GetType().AssemblyQualifiedName;
if (! string.IsNullOrEmpty(viewModelName))
{
viewModelName = viewModelName.Replace("View", "ViewModel");
var viewModel = DependencyLocator.GetInstance(Type.GetType(viewModelName));
if (viewModel != null)
{
((FrameworkElement) dependencyObject).DataContext = viewModel;
}
}
}
}
And use it like so :
<vm:ViewModelLocator.LocateFor>
<Binding RelativeSource="{RelativeSource Self}" />
</vm:ViewModelLocator.LocateFor>
It works perfect at runtime but I have lost design time location. I suspect it is because I am using the relative binding, I tried changing it to x:Static
but that doesn't work. Are there any suggested changes I could make that would give me back Blendability?
EDIT : About DependencyLocator, it is a static wrapper around Ninject:
using System;
using System.Collections.Generic;
/// <summary>
/// Defines an object that wraps around specific DI containers.
/// </summary>
public static class DependencyLocator
{
/// <summary>
/// Gets or Sets the function used for locating a single instance of a specifc type.
/// </summary>
public static Func<Type, object> GetInstance;
/// <summary>
/// Gets or Sets the function used for locating all instance of a specific type.
/// </summary>
public static Func<Type, IEnumerable<object>> GetAllInstances;
/// <summary>
/// Gets the implementation of the provided service.
/// </summary>
public static T Get<T>()
{
return (T)GetInstance(typeof(T));
}
/// <summary>
/// Gets all implementations of the provided service.
/// </summary>
public static IEnumerable<T> GetAll<T>()
{
return (IEnumerable<T>) GetAllInstances(typeof (T));
}
}
Which gets populated in an app_start class in app.xaml. It was designed like this for design time purposes and if I make a simple view model locator class using an instanced object defined in the View's xaml it does indeed get the correct values from DependencyLocator, it is only since I switched to Dependency Properties that this issue came up.