Correct way to not implement ConvertBack on a IMultiValueConverter

我的梦境 提交于 2020-01-06 04:41:28

问题


I have an object that implements IMultiValueConverter. It is used to bind visibility of a column based upon a particular permutation of a specific bool value and a specific enum value, both part of the bound data. The ConvertBack method self evidently has no meaning.

On a regular IValueConverter, I could return Binding.DoNothing, but that is not an object[] so won't compile.

I currently throw an exception, but that doesn't feel ideal. Is there a better way?


回答1:


The correct way to implement the ConvertBack method of an IValueConverter or an IMultiValueConverter that does not support back-conversion is to throw a NotSupportedException.

Returning Binding.DoNothing makes no sense, as the method should never be called anyway. But if it is ever called unexpectedly, you would rightly get an exception that tells you what went wrong.

public object[] ConvertBack(
    object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
    throw new NotSupportedException();
}


来源:https://stackoverflow.com/questions/50054267/correct-way-to-not-implement-convertback-on-a-imultivalueconverter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!