Steven
Steven

Reputation: 2208

silverlight grid background image

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

Answers (2)

Callum Rogers
Callum Rogers

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

Prince Ashitaka
Prince Ashitaka

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

Related Questions