Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8435215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:51:20+00:00 2026-06-10T06:51:20+00:00

I wrote a really simple ViewModel Locator : public static readonly DependencyProperty LocateForProperty =

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T06:51:21+00:00Added an answer on June 10, 2026 at 6:51 am

    This code is just clarifying what you are trying to achieve:

    // Use this to trigger break into debugger when debugging another instance of Visual Studio in order to debug the behaviour at design time.
    System.Diagnostics.Debugger.Break();
    
    if (dependencyObject != null)
    {
        var view = GetLocateFor( dependencyObject ); // or instead of this access the changed value through the eventargs
    
        var viewModelName = view.GetType().AssemblyQualifiedName;
    
        if (! string.IsNullOrEmpty(viewModelName))
        {
            viewModelName = viewModelName.Replace("View", "ViewModel");
            var viewModel = DependencyLocator.GetInstance(Type.GetType(viewModelName));
            if (viewModel != null)
            {
               ((FrameworkElement) view ).DataContext = viewModel;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this really simple c++ function I wrote myself. It should just strip
I have quite a simple question really. I wrote a servlet for suppliers to
Yesterday I wrote a simple Python program (really simple as shown below) to validate
I wrote this REALLY simple code: $foo=false; echo $foo;//It outputs nothing Why? Shouldn't it
For debugging i wrote a really simple Web-Application which contains only a simple JSP
I wrote a really simple class called Sack that holds some data in no
I'm really confused, I just wrote this x = 0 y = 42 #Can
I wrote a function in bash script. However, it's complaining about syntax. I really
Using MVC2, I have a simple ViewModel that contains a bool field that is
This will probably seem like a really simple question, and I am quite confused

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.