ImageBrush for Grid programmatically

c#, wpf

Solution

Try this instead:

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

RootGrid.Background = ib;

Also, this is obvious, but make sure the image is actually at the right path and set to be Content in the Project.

Problem

So I try to apply an image but cannot see any changes... What do I am missing? Thanks!! ``` 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; ```

Original source