Reputation: 2208
How do you set the backgound image of a grid in c# (code behind).
Thanks Sp Can I do something like this?
public ImageSource ImageSourcePin
{
set { this.DreamTypeImagePin.Background = value; }
}
This worked,thanks for your help
public String ImageSourcePin
{
set {
ImageBrush img = new ImageBrush();
img.ImageSource = (ImageSource)new ImageSourceConverter().ConvertFromString(value);
DreamTypeImagePin.Background = img;
}
}
Upvotes: 2
Views: 12521
Reputation: 15819
Try using an ImageBrush
. Place this inbetween your <Grid>
and </Grid>
tags.
<Grid.Background>
<ImageBrush ImageSource="Image.jpg"/>
</Grid.Background>
Imperatively, you could write this as:
ImageBrush imgBrush = new ImageBrush();
imgBrush.ImageSource = new BitmapImage(new Uri(@"folder\img.jpg", UriKind.Relative));
grid.Background = imgBrush;
Upvotes: 11
Reputation: 8773
ImageBrush img = new ImageBrush(); img.ImageSource = (ImageSource)new ImageSourceConverter().ConvertFromString("Image.jpg"); System.Windows.Controls.Grid g = new System.Windows.Controls.Grid(); g.Background = img;
HTH
Upvotes: 4