title: Silverlight: Nested containers and Binding
body: <h2>The Scenario Bit:</h2>
  
  <p>On one of the controls within my Silverlight application I have embedded a custom user control. Within this embedded control is another custom user control that contains a datagrid. I would like to use binding to populate the datagrid. Easy enough I just sepcificy a collection that is in the DataContext of the parent control. </p>
  
  <p><strong><em>Parent Form:</em></strong></p>
  
  <pre><code>&lt;UserControl x:Class="ParentControl"
               ...&gt;
      &lt;Grid x:Name="LayoutRoot" &gt;        
          &lt;ReusableControl /&gt;
      &lt;/Grid&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p><strong><em>Parent Codebehind:</em></strong></p>
  
  <pre><code>public partial class ParentControl: UserControl 
      {
  
          public ParentControl()
          {
              InitializeComponent();
              this.DataContext = ObjectCollection;
          }
  
          public ObservableCollection&lt;object&gt; ObjectCollection
          {
              get ;
              set ;
          }
      }
  </code></pre>
  
  <p><strong><em>Intermediate Form</em></strong></p>
  
  <pre><code>&lt;UserControl x:Class="ReusableControl"
               ...&gt;
      &lt;Grid x:Name="LayoutRoot" Background="Gold"&gt;        
          &lt;CustomDataGrid /&gt;
      &lt;/Grid&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p><strong><em>Child Control:</em></strong></p>
  
  <pre><code>&lt;UserControl x:Class="CustomDataGrid"
               ...&gt;
      &lt;Grid x:Name="LayoutRoot"&gt;        
          &lt;data:DataGrid x:Name="dgItems"
                         AutoGenerateColumns="True"
                         ItemsSource="{Binding ObjectCollection}"
                         &gt;
          &lt;/data:DataGrid&gt;
      &lt;/Grid&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <h2>The Question Bit:</h2>
  
  <p>I want to specificy the columns of the datagrid dynamically, based on another collection in the parent control DataContext. How can I do this? Is there more than one way of skinning this cat?*</p>
  
  <p>Thanks,
  Mark</p>
  
  <p>*No cats where harmed during the asking of this question.</p>