Well, I am pulling my hair out because of this, so any help would be greatly appreciated!
I am building a WPF application using the MVVM pattern.
In an attempt to retrieve data during development, I use the Ninject dependency injection infrastructure in conjunction with a service locator (as in the example in http://jonas.follesoe.no/YouCardRevisitedImplementingDependencyInjectionInSilverlight.aspx
(since I use WPF and not Silverlight, I check the development-time properties a little differently, but for the rest I think this applies).
I cannot get it to work during development, no matter what I do, the binding does not even seem called. It works great at runtime.
Here is the code for my Ninject module:
public class ViewModelModule : StandardModule { public override void Load() { bool isRuntime = !ViewModelBase.IsInDesignMode; if (isRuntime) { Bind<IViewModel>().To<MyViewModel>(); } else { Bind<IViewModel>().To<MyDesignTimeViewModel>(); } } }
MyDesignTimeViewModel is a simple CLR object that returns hardcoded data instead of all the same properties on MyViewModel .
The service locator is as follows:
public class ViewModelLocator { private static IKernel kernel; static ViewModelLocator() { if (kernel == null) { kernel = new StandardKernel(new ViewModelModule()); } } public IViewModel ViewModel { get { var vm = kernel.Get<IViewModel>(); return vm; } } }
And XAML binds the DataContext of the page as follows (although I tried several different ways to declare it, all with the same result):
<Page.DataContext> <Binding Source="{StaticResource viewModelLocator}" Path="ViewModel" /> </Page.DataContext>
(where viewModelLocator is declared in a ResourceDictionary, which is merged at the top of this file).
As I said, this works fine at runtime, even if I switch my Ninject binding to use MyDesignTimeViewModel at runtime, it successfully displays dummy data. I have a dummy converter in one of my bindings to see what goes through, and this is caused at runtime, but not at development time (I was desperate to debug a development time instance using a separate Visual Studio process all day, as recommended by MSDN !)
During development, Ninject binding continues with the kernel instance. Then viewModel is called and it returns DesignTimeViewModel along with all my hard-coded data. But the actual binding to any properties on the viewmodel never seems to be called (the breakpoint for the control converter never hits).
I really don't see what I'm doing wrong. Any pointers in any direction would be greatly appreciated, because at this point I am just puzzled. Thanks:)