[ACCEPTED]-ImageSourceConverter error for Source=null-ivalueconverter

Accepted answer
Score: 100

@AresAvatar is right in suggesting you use 8 a ValueConverter, but that implementation 7 does not help the situation. This does:

public class NullImageConverter :IValueConverter
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        if (value == null)
            return DependencyProperty.UnsetValue;
        return value;

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        // According to https://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter.convertback(v=vs.110).aspx#Anchor_1
        // (kudos Scott Chamberlain), if you do not support a conversion 
        // back you should return a Binding.DoNothing or a 
        // DependencyProperty.UnsetValue
        return Binding.DoNothing;
        // Original code:
        // throw new NotImplementedException();

Returning 6 DependencyProperty.UnsetValue also addresses the performance issues from 5 throwing (and ignoring) all those exceptions. Returning 4 a new BitmapSource(uri) would also get rid of the exceptions, but 3 there is still a performance hit (and it 2 isn't necessary).

Of course, you'll also 1 need the plumbing:

In resources:

<local:NullImageConverter x:Key="nullImageConverter"/>

Your image:

<Image Source="{Binding Path=ImagePath, Converter={StaticResource nullImageConverter}}"/>
Score: 13

I used Pat's ValueConverter technique and 4 it worked great. I also tried the TargetNullValue 3 technique, by flobodob from here, and it also 2 works great. It's easier and cleaner.

<Image Source="{Binding LogoPath, TargetNullValue={x:Null}}" />

TargetNullValue 1 is simpler, and requires no converter.

Score: 6

Bind your image directly on an object and 2 return "UnsetValue" if necessary

<Image x:Name="Logo" Source="{Binding ImagePath}"  />

The property 1 in your ViewModel :

    private string _imagePath = string.Empty;
    public object ImagePath 
            if (string.IsNullOrEmpty(_imagePath))
                return DependencyProperty.UnsetValue;

            return _imagePath;
            if (!(value is string)) 

            _imagePath = value.ToString();

More Related questions