alfah
alfah

Reputation: 2085

How to set background image to buttons in silverlight in the codebehind file of WP7 application

I would like tho know how to add an image to a button dynamically in the code rather than in xaml. Someone had suggested to proceed the following way

ImageBrush brush = new ImageBrush();
brush.ImageSource = new BitmapImage(new Uri(@"Images/myImage.png", UriKind.Relative)); 
AlphabetButton.Background = brush;

but BitmapImage is not detected by the intellisense though i can find BitConvertor and Bitmapcache. If this is not the method, how else would i set the background images

Upvotes: 2

Views: 7937

Answers (2)

Darkside
Darkside

Reputation: 1739

I find that it is much easier and simpler to just put an Image control behind the button that you want to display and leave the button with a transparent background.

This removes the need to create a separate brush for the button and also makes it more extensible if you want to add test over the image. IMHO

Upvotes: 1

Pieter-Bas
Pieter-Bas

Reputation: 354

The code:

ImageBrush background = new ImageBrush();
background.ImageSource = new System.Windows.Media.Imaging.BitmapImage(new Uri(@"SplashScreenImage.jpg", UriKind.Relative));
Button1.Background = background;

Works as expected. BitmapImage is in System.Windows.dll which should already be in your WP7 project.

Upvotes: 8

Related Questions