title: Unity Dependency Injection from usercontrol viewmodel to customUsercontrol Viewmodel
body: <p>I have a <code>UserControl</code> which is bound to a <code>viewmodel</code>. The <code>viewmodel</code> has parameters in the constructor which is done via dependency injection. The dependency injection is working fine up to this point.</p>
  
  <p>And then I have a <code>CustomUserControl</code> which is Used in my <code>UserControl1View</code>.
  How do I get the Dependency injection working in my <code>CustomUserControl</code>?</p>
  
  <p>I am new to dependency injection and did some research, but can't seem to get it working. I get an error on  </p>
  
  <pre><code>public partial class UserControl1View : UserControl, IView {
      public UserControl1View( ) {
          InitializeComponent( );
      }
  }  
  </code></pre>
  
  <p>This is the error:
  <a href="https://i.stack.imgur.com/Tp0aq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Tp0aq.png" alt="enter image description here"></a> </p>
  
  <p>Here is the example of the code.</p>
  
  <p><strong>UserControl</strong>:</p>
  
  <pre><code>public partial class UserControl1View : UserControl, IView {
      public UserControl1View( ) {
          InitializeComponent( );
      }
  }
  </code></pre>
  
  <p><strong>UserControlViewModel</strong>:</p>
  
  <pre><code>public class UserControl1ViewModel 
  {
      private readonly ISomeDataService dataService;
      public UserControl1ViewModel (ISomeDataService dataservice, IUnityContainer container ) 
      {
         //Please note that the Dependency injection still works in this class, to much to explain the whole structure.   
         this.dataService = dataservice;
         container.RegisterType( typeof( IView ), typeof( CustomUserControlView ) ); 
         var view = container.Resolve&lt;CustomUserControlView&gt;( );
      }
  </code></pre>
  
  <p><strong>XAML</strong>:</p>
  
  <pre><code>&lt;uc:CustomUserControlView/&gt;
  </code></pre>
  
  <hr>
  
  <p><strong>CustomUserControl</strong>:</p>
  
  <pre><code>public partial class CustomUserControlView : UserControl, IView 
  {
      public CustomUserControlView(IUnityContainer container) 
      {
          container.RegisterType( typeof( IViewModel ), typeof( CustomControlViewModel ) );
          var viewModel = container.Resolve&lt;CustomControlViewModel&gt;( );
          this.DataContext = viewModel;
          InitializeComponent( );
      }
  }
  </code></pre>
  
  <p><strong>CustomUserControlViewModel</strong>:</p>
  
  <pre><code>public partial class CustomUserControlViewModel : UserControl, IView 
  {
     private readonly ISomeDataService dataService;
     public CustomUserControlViewModel(ISomeDataService dataservice) 
     {
        var data = dataService.GetsomeCollection()
     }
  }
  </code></pre>