Derik Whittaker



Hey, why is my ViewModelLocator not being called when creating a view?

Ok, this is going to seem really stupid, cause it was, but I thought I would share my pain incase someone has the same issue.

A while back I blogged about how to create a Convention Based View Model Location using Ninject in a MVVM/Silverlight Application in this post I talk about how to create a VMLocator, but to be honest my issue would also be visible if you rolled your own so you do not need to review my prior post if you do not want.

My issue was that I created a child window in my Silverlight application and setup my binding for the VMLocator as normal

DataContext="{Binding Source={StaticResource VMLocator}, Converter={StaticResource VMIndexerConverter}}"

However, when I created the instance of the View the VMLocator was not being invoked and I was perplexed.  After talking it over w/ a co-worker it dawned on me that in my overloaded constructor in my View I did NOT setup the call to InitializeComponent(); correctly, OK i plan forgot to add it. Once I added the call to InitializeComponent(); everything just worked and the world was a happy place.

The moral of the story is this, if you are trying to use the VMLocator pattern via setting the DataContext in XAML you MUST make sure you have a call to InitializeComponent(); in your constructor or you will not get the results you expected.

Till next time,

Posted 08-04-2011 11:44 AM by Derik Whittaker
Filed under:



Test wrote re: Hey, why is my ViewModelLocator not being called when creating a view?
on 09-01-2011 4:43 AM


About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Google Reader or Homepage Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of
Red-Gate Tools For SQL and .NET


SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
NHibernate Profiler
Balsamiq Mockups
JetBrains - ReSharper
Web Sequence Diagrams
Ducksboard<-- NEW Friend!


Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers


Community Server (Commercial Edition)