C#WPF更改网格背景图像

时间:2015-06-05 15:55:41

标签: c# wpf

如何更改点击按钮的网格背景图片?我尝试了这段代码,但它没有用。我需要帮助。

代码:

WpfApplication5.Properties.Settings.Default.GridImage = "Pictures\file.jpg";

2 个答案:

答案 0 :(得分:1)

可以使用ImageBrush设置背景:

    var imgBrush = new ImageBrush();

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

使用相对路径时,您需要在 bin \ Debug文件夹中包含带有file.jpg的图片文件夹。

答案 1 :(得分:0)

喜欢这样。您应该设置Background

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