0

We have a WPF project, at some point we want to display a list of compatibles devices. Thoses devices comes from a registry in which different modules(projects) can add their library.

At the end, I'm trying to bind a string with the image to the Source of an an image:

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

Currently in "Image", I've this:

"/My.Project.Name;Component/Images/SomeImage.png"

Please note that :

  • "Images" is a folder in the mentionned project
  • "My.Project.Name" is only a library project, so it's not the WPF one
  • "SomeImage.png" is currently set as "Content".

It's what I found I should use, but currently I get this error:

System.Windows.Data Error: 6 : 'TargetDefaultValueConverter' converter failed to convert value '/My.Project.Name;Component/Images/SomeImage.png' (type 'String'); fallback value will be used, if available. BindingExpression:Path=Image; DataItem='WizardOneChoice`1' (HashCode=56913160); target element is 'Image' (Name=''); target property is 'Source' (type 'ImageSource') IOException:'System.IO.IOException: Cannot locate resource 'images/someimage.png'.
   at MS.Internal.AppModel.ResourcePart.GetStreamCore(FileMode mode, FileAccess access)
   at System.IO.Packaging.PackagePart.GetStream(FileMode mode, FileAccess access)
   at System.IO.Packaging.PackWebResponse.CachedResponse.GetResponseStream()
   at System.IO.Packaging.PackWebResponse.get_ContentType()
   at System.Windows.Media.Imaging.BitmapDecoder.SetupDecoderFromUriOrStream(Uri uri, Stream stream, BitmapCacheOption cacheOption, Guid& clsId, Boolean& isOriginalWritable, Stream& uriStream, UnmanagedMemoryStream& unmanagedMemoryStream, SafeFileHandle& safeFilehandle)
   at System.Windows.Media.Imaging.BitmapDecoder.CreateFromUriOrStream(Uri baseUri, Uri uri, Stream stream, BitmapCreateOptions createOptions, BitmapCacheOption cacheOption, RequestCachePolicy uriCachePolicy, Boolean insertInDecoderCache)
   at System.Windows.Media.Imaging.BitmapFrame.CreateFromUriOrStream(Uri baseUri, Uri uri, Stream stream, BitmapCreateOptions createOptions, BitmapCacheOption cacheOption, RequestCachePolicy uriCachePolicy)
   at System.Windows.Media.ImageSourceConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
   at MS.Internal.Data.DefaultValueConverter.ConvertHelper(Object o, Type destinationType, DependencyObject targetElement, CultureInfo culture, Boolean isForward)
   at MS.Internal.Data.TargetDefaultValueConverter.Convert(Object o, Type type, Object parameter, CultureInfo culture)
   at System.Windows.Data.BindingExpression.ConvertHelper(IValueConverter converter, Object value, Type targetType, Object parameter, CultureInfo culture)'

What am I missing?

J4N
  • 19,480
  • 39
  • 187
  • 340
  • https://social.msdn.microsoft.com/Forums/en-US/8d901984-e023-4063-93af-43c8e3f0826c/wpf-image-source-data-binding-from-a-string?forum=wpf – Gaterde Apr 23 '18 at 10:33
  • In code behind, you have to specify a full [Resource File Pack URI](https://learn.microsoft.com/en-us/dotnet/framework/wpf/app-development/pack-uris-in-wpf#resource-file-pack-uris), including the prefix: `pack://application:,,,/My.Project.Name;Component/Images/SomeImage.png`, where `My.Project.Name` is an assembly name. The Build Action of the image files must be set to `Resource`. – Clemens Apr 23 '18 at 10:41
  • @Clemens Why is it marked as clone? I don't want to have the image in the resource, and my image is in another project. Nothing in the reference helps me. – J4N Apr 23 '18 at 10:48
  • Sorry that the question is not an exact duplicate, but the answer exactly explains what to do. Just with a different folder name, Images instead of Resources. – Clemens Apr 23 '18 at 10:48
  • @Clemens I disagree, there is no indication on how to reference an external project(which apparently is different since in the given project there is no mention of which project it is used in. – J4N Apr 23 '18 at 10:50
  • Changed the duplicate... The important thing is to set the Build Action of the image files to `Resource` and use fully qualified resource file pack URIs. – Clemens Apr 23 '18 at 10:51
  • @Clemens this is in fact the question I initially refered to. Didn't see the "Update 3" – J4N Apr 23 '18 at 10:54

0 Answers0