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
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.
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
精彩评论