Unable to bind data at design time in WPF using MVVM - ViewModel property will never be called

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:)

+4
source share
2 answers

Here are a few things to try:

  • While you are debugging the constructor, check to see if you have any warnings / errors.
  • Try tracking your designer with the latest version of Snoop , which allows you to connect to Visual Studio.
  • Try debug bindings . Maybe something interesting comes out.

And don't forget to check DataContexts :).

Hope something helps.

Greetings

+1
source

Perhaps new design features are available for you for Visual Studio 2010 and Expression Blend 4.

How this works is shown in the BookLibrary example of the WPF Application Framework (WAF) . Download the .NET4 version.

0
source

Source: https://habr.com/ru/post/1301723/


All Articles