0

I'm trying to set the background of a dock panel in my WPF application. Originally I had the full path hard coded in the image source attribute, but realise this is not best practice.

I set up an images folder in my project, and added the background image there. The directory looks like:

Solution:
 -Resources
  -Background.png
 -Project
  -bin
  -obj
  -properties

How can I set the background with this setup? At the moment I have this, which doesn't give me an error before running, but then throws an exception when I do.

<DockPanel.Background>
    <ImageBrush ImageSource= "..\Resources\background.png"/>
</DockPanel.Background>

Thanks

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Stinkidog
  • 425
  • 2
  • 7
  • 19

1 Answers1

1

Try this:

<DockPanel.Background>
    <ImageBrush ImageSource= "Resources/background.png"/>
</DockPanel.Background>
Revan
  • 1,104
  • 12
  • 27
  • 2
    And set the Build Action of the image file to Resource. – Clemens Jul 26 '15 at 18:11
  • Nope, I get an xaml parse exception, how do i set the build action for the image? – Stinkidog Jul 26 '15 at 18:20
  • The Resources folder has to be part of the Visual Studio project. The image's Build Action can be set in the Properties Window (Context Menu -> Properties). – Clemens Jul 26 '15 at 18:28
  • There was my problem! My Resources folder wasn't in the project. Thank you. Sorry if that's simple, I literally started this today haha – Stinkidog Jul 26 '15 at 18:33
  • @Stinkidog Good to know that your problem solved. Always Resource folder should be the part of project. – Revan Jul 27 '15 at 08:24