title: Passing MainWindow instance to ViewModel
body: <p>In my WPF application I need to send do something with my MainWindow on the ViewModel. So my idea was to set <code>this.DataContext = new ViewModel (this)</code> in code-behind, and then in the constructor of the view-model I could just use the 'this' parameter that I had sent as the Window instance:</p>
  
  <pre><code>public ViewModel(Window Wndw){/../}
  </code></pre>
  
  <p>Now what I need to do is access a 'MainGrid' element that is inside my window:</p>
  
  <pre><code>&lt;Window bunch
          of
          stuff&gt;
      &lt;Grid x:Name="MainGrid"&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>But when I try something like:</p>
  
  <pre><code>Grid MGrid = Wndw.FindName("MainGrid") as Grid
  </code></pre>
  
  <p>I don't get any errors but it is always null, so I keep getting Null Reference Exception when I try to do anything with MGrid.</p>
  
  <p>On the other hand, the <strong>Wndw is not null</strong>, I tested that. And also, passing MainGrid instead of the whole Window is not an option.</p>
  
  <p>Any ideas?</p>