title: Injecting dependencies into WPF using Ninject
body: <p>I have a WPF UserControl that I'd like to inject dependencies into. What's the best way to do this with <a href="http://ninject.org/" rel="noreferrer">Ninject</a>?</p>
  
  <p>To take a concrete example: I have a UserControl called MapView, and I want to inject an instance of my IDialogueService into it, via either constructor or property injection. Currently, I'm not using any dependency injection, and my control is created in XAML:</p>
  
  <pre><code>&lt;Window x:Class="GameWindow" ...&gt;
      &lt;Grid Name="root"&gt;
          &lt;MapView x:Name="mapView" ... /&gt;
          &lt;!-- other stuff here --&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>Creating an IKernel, and binding the IDialogueService, is straightforward enough. But I'm stuck on how to use the kernel to inject dependencies into my MapView. I'm still a total newbie at Ninject, so maybe there's something obvious that I'm missing.</p>
  
  <p>I can think of several ways to solve this:</p>
  
  <ol>
  <li><p>Instantiate the MapView in code. Remove the <code>&lt;MapView ... /&gt;</code> from the XAML, and instead add this to GameWindow's constructor:</p>
  
  <pre><code>public GameWindow(IKernel kernel) {
      root.Children.Add(kernel.Get&lt;MapView&gt;());
  }
  </code></pre>
  
  <p>Downsides: extra complexity from not using XAML; GameWindow has a dependency on IKernel.</p></li>
  <li><p>Keep the instantiation in XAML, and use property injection instead:</p>
  
  <pre><code>public GameWindow(IKernel kernel) {
      kernel.Inject(mapView);
  }
  </code></pre>
  
  <p>Downside: Ninject docs say that Inject() "should not be used for most cases", so I don't even know if it does what I think it does, or whether it makes sense to use it here. And GameWindow still has a dependency on IKernel.</p></li>
  <li><p>Add injectable parameters/properties to GameWindow that pass the values along to the MapView (probably via properties on MapView), and then use <code>Get&lt;GameWindow&gt;()</code>. Downside: now I'm manually passing dependencies all over the place, which is what a DI framework is supposed to automate for me.</p></li>
  <li><p>After instantiating the GameWindow, walk the logical tree and call IKernel.Inject() on everything. Downsides: again, I don't know whether Inject() does what I think it does, or whether it's appropriate here. Caller must remember to do the walk-the-visual-tree-and-inject thing after instantiating a GameWindow.</p></li>
  </ol>
  
  <p>Is there a better way to do this? Maybe a WPF extension for Ninject that lets me do a <code>Get&lt;GameWindow&gt;()</code>, and automatically walks the logical tree for me (as in #4), doing property injection on everything? If such a thing doesn't exist, would it be possible for me to write it?</p>
  
  <p>How do you guys approach using Ninject with WPF? Do you use any of the above approaches (and if so, can you share up/downsides I don't know about)? Do you have better approaches?</p>