C# 网格的ImageBrush编程

C# 网格的ImageBrush编程,c#,wpf,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

所以我尝试应用图像,但看不到任何更改

我错过了什么?谢谢

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;
请尝试以下方法:

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

RootGrid.Background = ib;

此外,这是显而易见的,但请确保图像实际位于正确的路径,并设置为项目中的内容。

很抱歉,我有一个嵌入图像,您的代码给了我一个错误。错误是什么?什么是嵌入式图像?您是否将其设置为项目中的
?我将其设置为,并且它是DLL用户控制项目。确定我已修复它ImageBrush ib=新ImageBrush(新位图图像(新Uri)(@)pack://application:,,,/MyDllName;Component/Images/bg1.jpg”);@AcademyofProgrammer提出的解决方案非常有效!