title: Binding ItemSource of an element in a UserControl to an item in the parents ViewModel
body: <p>Sorry, I'm thrashing around a bit learning MVVM, WPF, and XAML simultaneously.</p>
  
  <p>I have a problem that I created and I am completely confused with how this should be handled in MVVM.</p>
  
  <p>What I have is a parent window that contains a user control that draws graphs.  The graph drawing XAML used to be part of the parent window, but I moved it to a user control for organization as the parent window was very big.</p>
  
  <p>In the parent windows XAML I have .....</p>
  
  <pre><code>&lt;Window ....&gt;
       &lt;Window.Resources&gt;
           &lt;ViewModel:DropGraphViewModel x:Key="myViewModel"/&gt;
       &lt;/Window.Resources&gt;
  
       &lt;!-- Set the data context to the view model. --&gt;
       &lt;Window.DataContext&gt;
           &lt;Binding Source="{StaticResource myViewModel}"/&gt;
       &lt;/Window.DataContext&gt;
       .....
  &lt;/Window&gt;
  </code></pre>
  
  <p>And then in the new user control XAML class I have a resource that is created that is a 'generator' class that serves up things that will be used by parts of the graph.  It looks like this...</p>
  
  <pre><code>&lt;UserControl ......
      &lt;!-- Graph resources --&gt;
          &lt;Grid.Resources&gt;
              &lt;!-- The binding here for ItemsSource represents the collection the graph will be bound to --&gt;
  
              &lt;!-- THIS LINE DOESN'T WORK ANYMORE  --&gt;
              &lt;Graphs:LineChartGenerator x:Key="generator" ItemsSource="{Binding Source={StaticResource myViewModel}, Path=SampleData}" Width="500"  Height="200"&gt; --&gt;
          &lt;/Grid.Resources&gt;
  </code></pre>
  
  <p>And then later on when I want to do some things like draw graph lines I used to reference the generator through binding.</p>
  
  <pre><code>    &lt;!-- Connect the points --&gt;
      &lt;Polyline Points="{Binding Source={StaticResource generator}, Path=Points}" Stroke="Blue" /&gt;
  </code></pre>
  
  <p>What is now broken now that I am in am using a nested user control is that when I create the instance of the 'generator' class in the resources, I can't pass in the binding that was <strong>ItemsSource="{Binding Source={StaticResource myViewModel}, Path=SampleData}"</strong> because I no longer have access to the view model (myViewModel) that is in the static resource of the parent window!  So I can't set the binding during the creation time of the resource like I used to do.</p>
  
  <p><strong>What is the proper MVVM way to handle this type of pattern?</strong></p>
  
  <p>How do I inject the ItemsSource into my new user control so that it can pass it in when it creates the LineChartGenerator class instance?</p>