ImageBrush for Grid以编程方式

时间:2012-05-16 19:13:29

标签: c# wpf

所以我尝试应用图片但看不到任何变化......

我缺少什么?谢谢!

BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.UriSource = new Uri(@"pack://application:,,,/Images/bg1.jpg", UriKind.RelativeOrAbsolute);
bi.EndInit();
ImageBrush ib = new ImageBrush();
ib.TileMode = TileMode.Tile;
ib.ImageSource = bi;
ib.Stretch = Stretch.None;
RootGrid.Background = ib;

1 个答案:

答案 0 :(得分:9)

请改为尝试:

var ib = new ImageBrush {
  ImageSource =
    new BitmapImage(
      new Uri(@"Images\bg1.jpg", UriKind.Relative)
    )
};

RootGrid.Background = ib;

此外,这很明显,但要确保图像实际上位于正确的路径并设置为项目中的内容。