-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";
4
  • I'm said "Background Image" not color Commented Jun 5, 2015 at 16:06
  • The question is unclear: please include XAML and C# code snippet related to that Button click event. Thanks and regards, Commented Jun 5, 2015 at 16:09
  • Rick I tried this but not worked. Commented Jun 5, 2015 at 16:34
  • Can you please post the error message you get and the code of your button Click event. Commented Jun 5, 2015 at 16:48

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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 );
        }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.