Usage with MVVM and WPF

936 views
Skip to first unread message

Brian

unread,
Sep 1, 2011, 1:28:19 PM9/1/11
to ninject
Hey there,

I read through the wiki and have tried to find examples of how to use
Ninject in a WPF application and haven't had much luck. I'm using
Ninject with MVC3 and it works great! I wire up the MVC Start and
declare my bindings and everything magically works.

Where I'm having confusion is how do I use Ninject in a WPF
application that implements the MVVM pattern. Basically what I can't
figure out is where I do set up my bindings?

Thanks

Remo Gloor

unread,
Sep 1, 2011, 1:50:47 PM9/1/11
to nin...@googlegroups.com

Hi Brian

Have a look at MPF frameworks like Caliburn micro. They usually provide support for IoC containers.

http://caliburnmicro.codeplex.com/

http://caliburnmicro.codeplex.com/discussions/230861

Remo

Remo Gloor

unread,
Sep 1, 2011, 1:51:52 PM9/1/11
to nin...@googlegroups.com
Sry I mean WPF frameworks

Michael Gerfen

unread,
Sep 1, 2011, 2:14:28 PM9/1/11
to nin...@googlegroups.com
1. If you are not using any MVVM framework here are some simple steps...

a. Create a class called ServiceLocator, like so...

public class ServiceLocator
{
private static IKernel kernel_;

static ServiceLocator()
{
kernel_ = new StandardKernel(< your module>)
}
}

b. Then in App.xaml, create a resource called ServiceLocator

<xxxx:ServiceLocator x:Key="ServiceLocator"
d:IsDataSource="True" />

c. Then wire up the correct view model in your XAML like so:

DataContext="{Binding YourViewModel, Source={StaticResource
ServiceLocator}}", where "YourViewModel" is a static property on the
ServiceLocator class.

2. If you are using MvvMLight, you can replace the service locator with your
own implementation as described above.

3. Caliburn.Micro has facilities for plugging in any IoC you like.

HTH,

Michael

Hey there,

Thanks

--
You received this message because you are subscribed to the Google Groups
"ninject" group.
To post to this group, send email to nin...@googlegroups.com.
To unsubscribe from this group, send email to
ninject+u...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/ninject?hl=en.


gerfen

unread,
Sep 1, 2011, 6:47:25 PM9/1/11
to ninject
BTW, I misspoke in 1.c. The property need not be a static property.

Brian

unread,
Sep 2, 2011, 9:31:06 PM9/2/11
to ninject
AWESOME! Thanks so much! We are not using an MVVM framework such as
Caliburn. We are using a homegrown simplified version and these
responses were exactly what I was needing!

Thanks!
Reply all
Reply to author
Forward
0 new messages