title: DataContext of usercontrol in WPF
body: <p>I'm new to WPF and I'm trying to start a little project with a maximum of good practice. I'm using MVVM and dependency injection.
  I have a concern which seems to be easy to understand but i can't find an answer (at this step, DataContext is not very clear for me).
  The UserControlView of type UserControl contains just a button for testing.</p>
  <p>This is the app class :</p>
  <pre><code>public App()
      {
          IServiceCollection services = new ServiceCollection();
          services.AddSingleton&lt;MainWindow&gt;();
          services.AddSingleton&lt;UserControlViewModel&gt;();
          services.AddSingleton&lt;UserControlView&gt;();
  
          _serviceProvider = services.BuildServiceProvider();
      }
  </code></pre>
  <p>The user control is included in the Main windows like that :</p>
  <pre><code>&lt;Grid&gt;
      &lt;views:UserControlView/&gt;
  &lt;/Grid&gt;
  </code></pre>
  <p>Now, in the OnStartup overrided method :</p>
  <pre><code>protected override void OnStartup(StartupEventArgs e)
      {
          MainWindow = _serviceProvider.GetRequiredService&lt;MainWindow&gt;();
          MainWindow.DataContext = _serviceProvider.GetRequiredService&lt;PaymentMeansViewModel&gt;();
          MainWindow.Show();
      }
  </code></pre>
  <p>Like that it works, my button is correctly binded to the command.</p>
  <p>But what is strange for me is that I have to set the 'UserControlViewModel' as the DataContext of the Main Window.
  Isn'it possible to bind it to the 'UserControlView', something like :</p>
  <pre><code> protected override void OnStartup(StartupEventArgs e)
      {
          MainWindow = _serviceProvider.GetRequiredService&lt;MainWindow&gt;();
  
          UserControlView testUC = _serviceProvider.GetRequiredService&lt;UserControlView&gt;();
          testUC.DataContext = _serviceProvider.GetRequiredService&lt;UserControlViewModel&gt;();
          MainWindow.Show();
      }
  </code></pre>
  <p>Thanks for help.</p>