【问题标题】:C# WPF Change grid background image [duplicate]C#WPF更改网格背景图像[重复]
【发布时间】:2015-08-20 15:47:16
【问题描述】:

如何更改单击按钮的网格背景图像?我试过这段代码,但没有用。我需要帮助。

代码:

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

【问题讨论】:

  • 我说的是“背景图片”而不是颜色
  • 问题不清楚:请包含与该按钮单击事件相关的 XAML 和 C# 代码 sn-p。谢谢和问候,
  • Rick 我试过了,但没用。
  • 能否请您发布您收到的错误消息和您的按钮单击事件的代码。

标签: c# wpf


【解决方案1】:

背景可以使用ImageBrush设置:

    var imgBrush = new ImageBrush();

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

使用相对路径时,bin\Debug文件夹中需要有图片文件夹和file.jpg。

【讨论】:

    【解决方案2】:

    像这样。你应该设置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 );
            }
    

    【讨论】:

      猜你喜欢
      • 2015-08-30
      • 1970-01-01
      • 2016-07-12
      • 1970-01-01
      • 2013-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多