title: Silverlight ImageButton UserControl
body: <p>I am just starting out with Silverlight (2 RC0) and can’t seem to get the following to work.  I want to create a simple image button user control.</p>
  
  <p>My xaml for the user control is as follows:</p>
  
  <pre><code> &lt;Button&gt;
          &lt;Button.Template&gt;   
              &lt;ControlTemplate&gt;
                  &lt;Image Source="{TemplateBinding ImageSource}" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" /&gt;
              &lt;/ControlTemplate&gt;
          &lt;/Button.Template&gt;
      &lt;/Button&gt;
  </code></pre>
  
  <p>The code behind is as follows:</p>
  
  <pre><code>public partial class ImageButtonUserControl : UserControl
      {
          public ImageButtonUserControl()
          {
              InitializeComponent();
          }
  
          public Image Source
          {
              get { return base.GetValue(SourceProperty) as Image; }
              set { base.SetValue(SourceProperty, value); }
          }
          public static readonly DependencyProperty SourceProperty = 
              DependencyProperty.Register("SourceProperty", typeof(Image), typeof(ImageButtonUserControl),null);
      }
  </code></pre>
  
  <p>I want to be able to dynamically create the ImageButtons and stuff them in a container like a WrapPanel:
  Assume we have an image named “image” already:</p>
  
  <pre><code>ImageButtonUserControl imageButton = new ImageButtonUserControl();
  imageButton.Source = image;
  this.thumbnailStackPanel.Children.Add(imageButton);
  </code></pre>
  
  <p>What do I need to do to get the image to display? I'm assuming I need to do something with DataContext, but I'm not quite sure what or where.</p>
  
  <p>Thanks for any help</p>