question

MarcJeeves-9615 avatar image
0 Votes"
MarcJeeves-9615 asked EmonHaque-1485 edited

WPF Binding Image Soure

Evening, I'm a little confused as to why this is not working i have a custom error handling window that has an image based on error, warning, information the model and view model(datacontext) are updating correctly and the binding path is correct but the image wont refresh

can anybody help

error message

Severity Count Data Context Binding Path Target Target Type Description File Line Project
Warning 1 SimpleNotification_ViewModel SimpleNotificationImagePath Image.Source, Name='Img_SimpleNotification' ImageSource Failed to convert value './Images/NikolaError.png' (type 'String') to the target type using converter 'TargetDefaultValueConverter'. The fallback value will be used if it's available. IOException:'System.IO.IOException: Cannot locate resource 'images/nikolaerror.png'.




110406-2021-06-29-19-17-32.png


windows-wpf
· 6
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


Maybe NikolaOk.png exists but NikolaError.png does not exist. Does it work if you temporarily remove the FallbackValue part?


0 Votes 0 ·

I can change the fall back to any on of the four images and they all work, so i know they exist.

Do IO have to do some conversion to make them a source object?

0 Votes 0 ·

Have you set Build Action -> Resource for those images?

0 Votes 0 ·

Content Copy If Newer

0 Votes 0 ·

@MarcJeeves-9615
Could you check if the NikolaError.png is locked? If it is locked, please unlock it. Sometimes, the picture itself may be problematic, I suggest you create a new NikolaError.png.

0 Votes 0 ·

Try with Resource and see whether it works. Do you need ./ before Images?

0 Votes 0 ·
MarcJeeves-9615 avatar image
0 Votes"
MarcJeeves-9615 answered

So the mystery deepens, after reading a couple of posts i decided to convert it to a bitmap and bind to a bitmap property,

it still did not work, but when i pause the code and inspect the bitmap and then allow the code to continue to then works???? WTF.

I'm guessing something is being lazily loaded somewhere by WPF, but this is just a guess


110648-2021-06-30-8-06-20.png



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MarcJeeves-9615 avatar image
0 Votes"
MarcJeeves-9615 answered MarcJeeves-9615 edited

I also tried to use a convertor but i have the same issue if i dont inspect the bitmap conversion the image is not shown if i pause the code and look at the bitmap then it shows

 <Image 
                             x:Name="Img_SimpleNotification"
                             Source="{Binding SimpleNotificationImage,Converter={StaticResource StringtoBitmapImageConvertor}}"
                             Height="100" 
                             Width="100" 
                             Margin="10,10,10,10">
                         </Image>
 using System;
 using System.Globalization;
 using System.Windows.Data;
 using System.Windows.Media.Imaging;
    
 namespace UI_Catalog.Convertors
 {
     public class StringtoBitmapImageConvertor : IValueConverter
     {
         public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
         {
             if (value != null)
             {
                 string imagename = value as string;
                 BitmapImage bitmapImage = new BitmapImage(new Uri(imagename, UriKind.Relative));
                 return bitmapImage;
             }
             return null;
         }
    
         public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
         {
             throw new NotImplementedException();
         }
     }
 }
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.