views:

106

answers:

2

Hi,

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;
        }
    }
+3  A: 

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;
Callum Rogers
I need to do it in code behind.
Steven
+2  A: 

Hi Try this,

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

Avatar