Best practice when not implementing IValueConvert.ConvertBack

前端 未结 4 1565
难免孤独
难免孤独 2020-12-30 19:24

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

相关标签:
4条回答
  • 2020-12-30 20:09

    According Microsoft, you should return DependencyProperty.UnsetValue

    0 讨论(0)
  • 2020-12-30 20:21

    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();
        }
    }
    
    0 讨论(0)
  • 2020-12-30 20:22

    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.

    0 讨论(0)
  • 2020-12-30 20:27

    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.

    0 讨论(0)
提交回复
热议问题