0

I've added an image to my Resources.resx file. I reference it like this:

<Image Source="{Binding MyImage}"/>

In the designer, I do see a squiggly blue line under the above. It says

The resource "MyImage" could not be resolved.

At runtime, it throws this error:

Provide value on 'System.Windows.Markup.StaticResourceHolder' threw an exception.

Any ideas what I'm doing wrong?

4thSpace
  • 43,672
  • 97
  • 296
  • 475
  • 1
    Is `MyImage` in your binding context? What does your model look like? Did you mean to use `StaticResource` or `DynamicResource` instead of Binding? – vcsjones Sep 07 '16 at 16:33
  • No - it isn't in the binding context. I'm pretty sure this is `StaticResource`. – 4thSpace Sep 07 '16 at 16:36

1 Answers1

3

Don't use resx files with WPF, you'd have to add some code to make the interop work. Images in resx files are primarily used for WinForms code.

Set the Build Action to Resource in the file properties:

build action

Then just reference the file directly: <Image Source="../../Some/Relative/Path.png"/>

Neither {Binding} nor {StaticResource} is needed there.

Lucas Trzesniewski
  • 50,214
  • 11
  • 107
  • 158
  • If the Build Action is Resource, there is no "relative path" that starts with "../". Actually, it is a [Resource File Pack URI](https://msdn.microsoft.com/en-us/library/aa970069(v=vs.110).aspx) without the `pack://application:,,,/` prefix, which is automatically added by the XAML parser. – Clemens Sep 07 '16 at 19:11
  • @Clemens you can also use a path relative to the xaml file if the resource is in the same assembly. I used `../` to indicate a parent directory. – Lucas Trzesniewski Sep 07 '16 at 21:09