5

how can I programmatically set the background of a button to be an image? I know how to do it in XAML, but in code, I keep getting stuck, I tried

Button.Background = new ImageBrush{ ImageSource = "source" };

but then I get the error that string cannot be converted to ImageSource.

GeekPeek
  • 1,615
  • 1
  • 20
  • 34

1 Answers1

13
Try:
Button.Background = new ImageBrush{ ImageSource = new BitmapImage(new Uri(imgPath, UriKind.Relative)) };
Divya
  • 2,594
  • 1
  • 17
  • 29
  • Thx =3 I already found an answer though: Background = new ImageBrush { ImageSource = ( ImageSource ) new ImageSourceConverter( ).ConvertFromString( "source" ) } – GeekPeek Jan 20 '12 at 10:32
  • +1 Note: if the image is from internet then we have to use Absolute. – Ganapathy C Apr 19 '12 at 11:15