WPF Binding Image Soure

Marc Jeeves 386 Reputation points
2021-06-30T02:25:21.343+00:00

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 Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,663 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Marc Jeeves 386 Reputation points
    2021-06-30T15:10:26.377+00:00

    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

    0 comments No comments

  2. Marc Jeeves 386 Reputation points
    2021-06-30T15:34:44.327+00:00

    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();
            }
        }
    }
    
    0 comments No comments