Best practice when not implementing IValueConvert.ConvertBack

前端 未结 4 1564
难免孤独
难免孤独 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: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.

提交回复
热议问题