-3

How can I change grid background image of clicked button? I tried this code but it didn't work. I need help.

Code:

WpfApplication5.Properties.Settings.Default.GridImage = "Pictures\file.jpg";
voytek
  • 2,202
  • 3
  • 28
  • 44

2 Answers2

1

Background can be set by using ImageBrush:

    var imgBrush = new ImageBrush();

    imgBrush.ImageSource = new BitmapImage(new Uri(@"Pictures\file.jpg", UriKind.Relative));
    myGrid.Background = imgBrush;

When using relative path, you need to have Pictures folder with file.jpg in bin\Debug folder.

Panu Oksala
  • 3,245
  • 1
  • 18
  • 28
0

Like this.You should set Background of Button

 <Grid>
      <Button Name="button1" Click="button1_Click">
      </Button>
  </Grid>

 private void button1_Click(object sender, RoutedEventArgs e)
        {
            Uri uri = new Uri("image path", UriKind.Relative);
            BitmapImage img = new BitmapImage(uri);
            button2.Background = new ImageBrush(img );
        }
zhangyiying
  • 414
  • 4
  • 15