This project is read-only.

Importing the Container

May 31, 2013 at 7:13 PM
I have a custom converter I made that I want to be able to call a method on one of my VMs to get a value for conversion.

I added:

[Import]
public IDependencyInjectionContainer Container { get; set; }

As a member to the Converter class but the Container is null, is there another way to import this?
May 31, 2013 at 8:15 PM
Hi KJ,

Just so I'm sure we are on the same page, you've created a custom converter you are using in your data binding (it implements IValueConverter)?

This is some what of a problem because of the way data converters are designed in WPF. Using normal data binding WPF will instantiate the data converter with no knowledge of the DI container (hence why you are seeing the null property) and set it into the the binding. This works well for most cases but for your case this doesn't really work.

To get around this you can always access the main DI container for the application though the Bootstrapper.Instance.Container property.

IDependencyInjectionContainer container = Bootstrapper.Instance.Container;

This solves half your problem, the other half of your problem is going to be accessing your ViewModel, I have a couple ideas on how to address this but they are all dependent on how you are using your ViewModel. Are your ViewModels marked with [Share] (by default they aren't and I usually don't but some people like to)?

Let me know and we can discuss your options further.
-Ian
May 31, 2013 at 10:53 PM
I have my VMs marked with [Shared(Permanent=true)] (This is a Windows 8 App by the way)

I tried to add

IDependencyInjectionContainer container = Bootstrapper.Instance.Container; to the Converter (and yes this is a IValueConverter) and it failed to find my StartPage on launch, even though I have a VM marked with [StartPage]. Removing the declaration fixed the problem but now I'm back to not having to container in the converter.
May 31, 2013 at 10:59 PM
That's odd, would you be interested in zipping up the project and emailing it to me ipjohnson123 at yahoo.com

I would have thought this would work so I'm curious as to why it's not.
Jun 1, 2013 at 12:24 AM
I took and did a quick test app where I created a custom IValueConverter and located the ViewModel successfully. So it must be something particular about your scenario.

Email me your project or a sample one if you are able to break it down into a simpler solution.
Jun 2, 2013 at 9:05 PM
I actually was able to fix the issue by not setting the container as a member and just getting it from the Bootstrapper in the Convert method.