Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I inject dependencies to user controls in WPF

What is the best way to transparently inject dependencies (using IOC container) to user controls in WPF?

I assume that user controls are part of XAML for the window or other user controls. Also I think parent (whoever it is) should not be responsible for this. Solution for manually injecting dependencies from parent looks not clean enough for me. I want to avoid managing the dependencies of my components explicitly as it voilates the idea of IOC.

Is any event which is raised when logical tree is being created so I can intercept it and inject my dependencies?

EDIT: by dependencies I also mean ViewModel, Controller, Presenter (whatever pattern is used)

Thanks, Andrey

like image 875
andrey.tsykunov Avatar asked Mar 09 '10 12:03

andrey.tsykunov


1 Answers

The best way to deal with dependencies in WPF is by following the MVVM pattern.

In short, you don't inject dependencies directly into User Controls (View), but rather into their DataContext (ViewModel).

like image 160
Mark Seemann Avatar answered Sep 28 '22 05:09

Mark Seemann