title: Dependency Injection In View throws system.nullreferenceexception
body: <p>Users are pointing me to <a href="https://stackoverflow.com/questions/37206055/unity-dependency-injection-from-usercontrol-viewmodel-to-customusercontrol-viewm">Unity Dependency Injection from usercontrol viewmodel to customUsercontrol Viewmodel</a></p>
  
  <p>The top answer says <code>Don't use DI/IoC container to inject controls</code></p>
  
  <p>If what im doing should NOT be done, at least advise on what i SHOULD do.</p>
  
  <p>I need my 2 <code>ViewModel</code> to know when my grid objects are checked/unchecked, and which object it was.</p>
  
  <p>How can this be done without <code>IEventAggregator</code>?</p>
  
  <hr>
  
  <p>I have a <code>usercontrol</code> that i am using as a <code>GridViewRow</code> template. This <code>usercontrol</code> is not loaded until users search for something and my grid displays objects.</p>
  
  <p>On the template, there are checkboxes that when checked, I need to communicate to my <code>viewmodel</code>.</p>
  
  <p>Currently when I build, my application works as expected. </p>
  
  <p>When i modify my <code>constructor</code> from <code>public MainGridTemplate()</code> to  <code>public MainGridTemplate(IEventAggregator ea)</code> and run the app without any other code changes (not even saving to a local variable), I get the error</p>
  
  <blockquote>
    <p>System.NullReferenceException: 'Object reference not set to an instance of an object.'</p>
  </blockquote>
  
  <p>My callstack only shows 1 entry, <code>[External Code]</code> and my breakpoint on the constructor is never hit. This leads me to believe the <code>IEventAggregator</code> that gets inject is null, or something is null along the path of injecting.</p>
  
  <p>In my view model, im injecting <code>IEventAggregator</code> as well as a <code>IRepository</code> service and those work without issues, so why would i get this error when using DI in a <code>usercontrol</code></p>
  
  <p><a href="https://i.stack.imgur.com/K4X6T.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/K4X6T.png" alt="enter image description here"></a></p>
  
  <pre><code>    public MainGridTemplate()
      {
          InitializeComponent();
  
          PromoFromDateTextBox.DropDownOpened += HandleClicks;
          PromoToDateTextBox.DropDownOpened += HandleClicks;
  
          PromoNumberTextBox.GotFocus += HandleClicks;
          PromoPriceTextBox.GotFocus += HandleClicks;
  
          AdsCheck.Click += HandleClicks;
          DistroCheck.Click += HandleClicks;
          ShowCheck.Click += HandleClicks;
          KeepCheck.Click += HandleClicks;
      }
  </code></pre>
  
  <p>My template is applied with the following in xaml</p>
  
  <pre><code>        &lt;ControlTemplate x:Key="PMSMainRowTemplate"
                           TargetType="telerik:GridViewRow"&gt;
              &lt;template:MainGridTemplate DataContext="{Binding}" /&gt;
          &lt;/ControlTemplate&gt;
          &lt;Style TargetType="telerik:GridViewRow"
                 BasedOn="{StaticResource GridViewRowStyle}"&gt;
              &lt;Setter Property="Template"
                      Value="{StaticResource PMSMainRowTemplate}" /&gt;
          &lt;/Style&gt;
  </code></pre>
  
  <h2>My Current Workaround:</h2>
  
  <p>In my bootstrapper class, ive added a property <code>StaticContainer</code></p>
  
  <pre><code>public static IUnityContainer StaticContainer {get; set; }
  </code></pre>
  
  <p>and in my usercontrol</p>
  
  <pre><code>var container = Bootstrapper.StaticContainer;
  _ea = container.Resolve&lt;IEventAggregator&gt;();
  </code></pre>
  
  <p>yay statics (not really...)</p>