Just wondering what people think is the best practice when implementing an IValueConverter which does not have a meaningfull ConvertBack implementation (or one that is only
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();
}
}