4

this is perplexing, in winforms this is increadibly easy however in WPF this seems impossible.

I need to set the background of a grid to an image, should be simple I thought.

The image has been set as a Resource (right click on the project name -> properties -> resources tab -> import existing file) but when i click the Background property and select tile brush it points me to the file that is imported into the Resources folder, this works until the app is ran from outside of visual studio where it doesn't work.

<Grid Width="550" Height="350">
    <Grid.Background>
        <ImageBrush ImageSource="Resources/CINTRA2016.png"/>
    </Grid.Background>

I have the above code in XAML, how do I work with resources? I have also tried <ImageBrush ImageSource="pack://application:,,,/CINTRA 2016;CINTRA2016"/> which didn't work.

Both images have a build action of Resource in Solution Explorer

Neo
  • 2,305
  • 4
  • 36
  • 70
  • 2
    Properties->Resources is not the usual way to add image resources in WPF. See e.g. [this answer](http://stackoverflow.com/a/25714375/1136211) or [this one](http://stackoverflow.com/a/22957974/1136211) for the correct approach. – Clemens Dec 22 '15 at 13:56

1 Answers1

5

Your path is probably wrong. Try using

<Grid Width="550"
      Height="350">
    <Grid.Background>
        <ImageBrush ImageSource="pack://application:,,,/WpfApplication1;component/Resources/CINTRA2016.png" />
    </Grid.Background>
</Grid>

where WpfApplication1 is the name of your Project and Resources the folder containing the image.

dontbyteme
  • 1,221
  • 1
  • 11
  • 23
  • Oh you must have the component part in it. thanks Microsoft for making another simple task stupid! – Neo Dec 22 '15 at 13:40
  • The `pack://application:...` prefix is usually not necessary in XAML, as it is prepended by default. The fact that this solves OP's problem must have some other reason. If the image file would have been properly added as WPF Image Resource, it's sufficient to write `ImageSource="Resources/CINTRA2016.png"`. That easy. – Clemens Dec 22 '15 at 14:14