title: Help understanding relationship of Resources/Styles/XAML files in Silverlight
body: <p>I'm new to Silverlight and I wanted to accomplish a relatively simple task:<br>
  &nbsp;&nbsp;&nbsp;Create a "panel" control that would display a heading and some child content.</p>
  
  <p>I was able to get this work to a degree, but the placement of the XAML really confuses me.</p>
  
  <p>On my page I use my control. This results in my panel being shown with a button in the child content area which is blue, with a 20px yellow heading at the top saying "Below is some content".</p>
  
  <pre><code>&lt;UserControl x:Class="SilverlightApplication9.Page"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:local="clr-namespace:SilverlightApplication9"&gt;
      &lt;Grid Background="White"&gt;        
          &lt;local:MyPanel Background="Blue"&gt;            
                  &lt;Button Width="50" Height="25" Content="Hello"&gt;&lt;/Button&gt;            
          &lt;/local:MyPanel&gt;    
      &lt;/Grid&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>My panel source code is simple, just:</p>
  
  <pre><code>public partial class MyPanel : ContentControl
  {
      public MyPanel()
      {
          DefaultStyleKey = typeof(MyPanel);   
          InitializeComponent();         
      }
  }
  </code></pre>
  
  <p>It's a partial class, and there is an attached XAML file, which is where my confusion starts:</p>
  
  <p>If I try placing my style/template code into the partial class XAML file, it seems to be ignored (my button is shown, but the other content like the colors and text is missing)</p>
  
  <pre><code>&lt;ContentControl x:Class="SilverlightApplication9.MyPanel"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:local="clr-namespace:SilverlightApplication9"&gt;
      &lt;ContentControl.Resources&gt;
          &lt;ResourceDictionary&gt;
              &lt;Style TargetType="local:MyPanel"&gt;
                  &lt;Setter Property="Template"&gt;
                      &lt;Setter.Value&gt;
                          &lt;ControlTemplate TargetType="local:MyPanel"&gt;
                              &lt;Grid Background="Yellow"&gt;
                                  &lt;Grid.RowDefinitions&gt;
                                      &lt;RowDefinition Height="20" /&gt;
                                      &lt;RowDefinition /&gt;
                                  &lt;/Grid.RowDefinitions&gt;
                                  &lt;TextBlock HorizontalAlignment="Center" Height="20" Grid.Row="0" Text="Below is some content"/&gt;
                                  &lt;Grid Grid.Row="1" Background="LightBlue"&gt;
                                      &lt;ContentPresenter /&gt;
                                  &lt;/Grid&gt;
                              &lt;/Grid&gt;
                          &lt;/ControlTemplate&gt;
                      &lt;/Setter.Value&gt;
                  &lt;/Setter&gt;
              &lt;/Style&gt;
          &lt;/ResourceDictionary&gt;
      &lt;/ContentControl.Resources&gt;
  &lt;/ContentControl&gt;
  </code></pre>
  
  <p>However if I create a \Themes\generic.xaml file and paste in the same code, it works</p>
  
  <pre><code>&lt;ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:SilverlightApplication9"
    xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows"
    &gt;
      &lt;Style TargetType="local:MyPanel"&gt;
          &lt;Setter Property="Template"&gt;
              &lt;Setter.Value&gt;
                  &lt;ControlTemplate TargetType="local:MyPanel"&gt;
                      &lt;Grid Background="Yellow"&gt;
                          &lt;Grid.RowDefinitions&gt;
                              &lt;RowDefinition Height="20" /&gt;
                              &lt;RowDefinition /&gt;
                          &lt;/Grid.RowDefinitions&gt;
                          &lt;TextBlock HorizontalAlignment="Center" Height="20" Grid.Row="0" Text="Below is some content"/&gt;
                          &lt;Grid Grid.Row="1" Background="LightBlue"&gt;
                              &lt;ContentPresenter /&gt;
                          &lt;/Grid&gt;
                      &lt;/Grid&gt;
                  &lt;/ControlTemplate&gt;
              &lt;/Setter.Value&gt;
          &lt;/Setter&gt;
      &lt;/Style&gt;
  &lt;/ResourceDictionary&gt;
  </code></pre>
  
  <p>I'm clearly missing something important about how resources or XAML files are processed or used in a project (I did not have any experience with WPF before Silverlight).</p>
  
  <p>What am I doing wrong that is preventing me from just putting the panel's template code into the panel's xaml file? Are there some concepts regarding XAML and resources that I'm getting wrong?</p>