Best practice when not implementing IValueConvert.ConvertBack

When ConvertBack contains no functionality, and you are not expecting it to be called, throw a NotImplementedException. It shouldn't have been called and therefore you want a run time exception.

If ConvertBack is intentionally being called, then you had better provide an implementation for it. One option is just to return DependencyProperty.UnsetValue, or handle exceptions within your ConvertBack implementation by returning DependencyProperty.UnsetValue.

My justification for this would be: returning a DependencyProperty.UnsetValue instead of throwing a NotImplementedException makes it unobvious when a ConvertBack method is being invoked when you really never intended it to be. Maybe it should have some functionality now that it is being invoked and throwing a run time exception. It would be much harder to discover the missing ConvertBack functionality if it is just returning DependencyProperty.UnsetValue.


I agree with @Todd White's answer.

Additionally, in order to save time, you can implement a base converter class which implements ConvertBack for you so you don't have to implement it every time saving duplicate code.

Technically, you don't have to override Convert either; But it has to be implemented in ConverterBase since it implements all methods of the IValueConverter interface. In practice, you will be overriding Convert every time and ConvertBack can be ignored most of the time.

public class ConverterBase : IValueConverter
{
    public virtual object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return DependencyProperty.UnsetValue;
    }
    public virtual object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return DependencyProperty.UnsetValue;
    }
}

public class VisibilityConverter : ConverterBase
{
    public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return ((bool)value ^ (parameter as bool? == true)).ToVisibility();
    }
}

According Microsoft, you should return DependencyProperty.UnsetValue


The documentation for IValueConverter.ConvertBack recommends returning DependencyProperty.UnsetValue.

The data binding engine does not catch exceptions that are thrown by a user-supplied converter. Any exception that is thrown by the ConvertBack method, or any uncaught exceptions that are thrown by methods that the ConvertBack method calls, are treated as run-time errors. Handle anticipated problems by returning DependencyProperty.UnsetValue.