title: Dynamically created button in WPF
body: <p>I'm try to do a bit of WPF, only really done windows forms until now and not a lot of that...</p>
  
  <p>All I'm trying to do is to dynamically within code (not xaml) set a button to show an image and set the size of the button to auto size to the image.</p>
  
  <p>The code below loads the image but it goes when the mouse is over the button and the button doesn't auto size to the image.</p>
  
  <p><code>tbButtonPicture</code> contains a local path on the PC to a bitmap e.g. <code>C:\temp\my Artwork\test1.bmp</code></p>
  
  <p>This what I have thus far which sits inside a loop:</p>
  
  <pre><code>Console.WriteLine(tbButtonPicture);
  System.Windows.Controls.Button newBtn = new Button();
  //newBtn.Content = i.ToString();
  newBtn.Background = new ImageBrush(new BitmapImage(new Uri(BaseUriHelper.GetBaseUri(this), tbButtonPicture)));
  newBtn.Name = "Button" + i.ToString();
  sp.Children.Add(newBtn);
  
  i++;
  </code></pre>