title: Reusing a customized style in Silverlight
body: <p>By modifying the <code>RowStyle</code> of a <code>DataGrid</code> I have created a customized grid that will display some buttons at the end of the row when the mouse hovers above the row:</p>
  
  <p><img src="https://i212.photobucket.com/albums/cc320/liversage/Stack%20Overflow/custom-datagrid.png" alt="Custom DataGrid"></p>
  
  <p>I created a new style for <code>DataGridRow</code> based on the default style. I then modified the XAML to add my buttons inside a <code>StackPanel</code> (details omitted):</p>
  
  <pre><code>&lt;UserControl.Resources&gt;
    &lt;Style x:Key="DataGridRowStyle" TargetType="swcd:DataGridRow"&gt;
      &lt;Setter Property="Template"&gt;
        &lt;Setter.Value&gt;
          &lt;ControlTemplate TargetType="swcd:DataGridRow"&gt;
            ...
            &lt;StackPanel x:Name="RowControlsPanel"&gt;
              &lt;Button&gt;
                ... these are the buttons displayed on the row
  </code></pre>
  
  <p>The <code>DataGrid</code> is modified using the style:</p>
  
  <pre><code>&lt;swcd:DataGrid RowStyle="{StaticResource DataGridRowStyle}"&gt;
    ...
  &lt;/swcd:DataGrid&gt;
  </code></pre>
  
  <p>I want to create another grid in a similar manner, but with a different set of buttons at the end of the row. I could create a textual copy of my style and modify it accordingly, but I was hoping that I could create a proper reusable class. I'm not sure how to approach this since the stuff I want to factor out of my style is a collection of controls (buttons) inside a style.</p>
  
  <p>My approach so far is to create a <code>MyDataGrid</code> class derived from <code>DataGrid</code>. I have added a new property <code>RowControls</code> to <code>MyDataGrid</code> enabling me to instantiate it like this:</p>
  
  <pre><code>&lt;local:MyDataGrid&gt;
    &lt;local:MyDataGrid.RowControls&gt;
      &lt;Button&gt;
         ... these controls should go at the end of the row
    &lt;/local:MyDataGrid.RowControls&gt;
    ...
  &lt;/local:MyDataGrid&gt;
  </code></pre>
  
  <p><code>MyDataGrid</code> uses a <code>RowStyle</code> as described above. But how do the contents of the <code>MyDataGrid.RowControls</code> collection get into the <code>Content</code> of <code>RowControlsPanel</code> in the style? I think I should do that in <code>OnApplyTemplate</code> of the <code>DataGridRow</code>, but then I need to derive a new <code>MyDataGridRow</code> class from <code>DataGridRow</code>. Unfortunately it seems that <code>DataGrid</code> is hardcoded to use <code>DataGridRow</code> and I'm not able to inject my own derived row class. I get the feeling that I need to approach my problem of reuse in a different manner, but I'm not sure how?</p>
  
  <p>Customizing simple controls like buttons by adding new properties and modifying the control template is quite easy, but how do I customize a complex control like <code>DataGrid</code> where the template I need to customize is nested inside the grid?</p>