I am developing window phone 7 application. I am new to the window phone 7 application. In my application I have dynamically created the button 开发者_高级运维control & added the background image to button control as follows
Button AlphabetButton = new Button();
AlphabetButton.Content = vAlphabet;
ImageBrush brush = new ImageBrush();
brush.ImageSource = new BitmapImage(new Uri("button_off.png", UriKind.Relative));
//brush.Stretch = Stretch.None;
AlphabetButton.Background = brush;
AlphabetButton.BorderBrush = new SolidColorBrush(Colors.Gray);
AlphabetButton.Margin = new Thickness(-12, -27, 0, 0);
AlphabetButton.Width = 80;
AlphabetButton.Height = 80;
I can see the background image to the button control but it does not fit in the space occupied by the button control. I can see the empty space on the right & bottom border of the button control. This is because the image does not fit into the space occupied by button control. How should I add the image in the background so that it can fit into the space occupied by the button control. Can you please provide me any code or link through which I can resolve the above issue ?
Change the commented out line of code to set the Stretch
to Fill
:
brush.Stretch = Stretch.Fill;
Could it be that this empty space is because you have set the margin to -12. -27, 0, 0?
The image will fill the area of the button (80x80) but then the whole button is shifted left 12 pixels and up 27 pixels, that creates some blank space on the right and bottom that you weren't expecting.
I suspect you will need to add an Image to the content of the button that has the size 92, 107 get the appearance you are after, its quite messy especially in code.
You can do this in the xaml. And for the Button content use grid instead of stackpanel else you will have a image that is not completely displayed
<Button>
<Button.Content>
<Grid>
<Image x:Name="buttonIcon"
Source="{Binding Image}"
Stretch="Fill">
</Image>
</Grid>
</Button.Content>
</Button>
精彩评论